Dear David,
Understanding OAuth in a Nutshell
OAuth allows apps to access email services like Gmail securely by asking users for permission, instead of relying on username/password combinations. With Gmail:
1. You register your app with Google.
2. Google provides a client ID and secret for your app.
3. Your app uses these credentials to request an "access token" from Google.
4. The token is then used to send emails via Gmail.
Steps to Implement OAuth in a Harbour Application
1. Register Your App with Google
Go to the Google Cloud Console.
Create a new project or use an existing one.
Enable the Gmail API for your project.
Set up OAuth consent screen (fill in basic details about your app).
Create OAuth 2.0 client credentials (choose "Desktop app" or similar).
Once done, you'll get:
A Client ID
A Client Secret
Pseudo-code
PROCEDURE Main()
LOCAL clientId := "YOUR_CLIENT_ID"
LOCAL clientSecret := "YOUR_CLIENT_SECRET"
LOCAL token := AuthorizeWithGoogle(clientId, clientSecret)
IF !Empty(token)
SendEmail(token)
ELSE
? "Authorization failed."
ENDIF
RETURN
FUNCTION AuthorizeWithGoogle(clientId, clientSecret)
LOCAL authUrl := "https://accounts.google.com/o/oauth2/auth?...&client_id=" + clientId
LOCAL tokenUrl := "https://oauth2.googleapis.com/token"
LOCAL authorizationCode
LOCAL accessToken
// Open authUrl in browser and get authorizationCode
RunBrowser(authUrl)
authorizationCode := GetCodeFromUser()
// Exchange authorizationCode for accessToken
accessToken := RequestAccessToken(tokenUrl, clientId, clientSecret, authorizationCode)
RETURN accessToken
FUNCTION SendEmail(token)
LOCAL smtpServer := "smtp.gmail.com"
LOCAL smtpPort := 587
LOCAL fromEmail := "youremail@gmail.com"
LOCAL toEmail := "recipient@gmail.com"
LOCAL subject := "Test Email"
LOCAL body := "This is a test email sent via OAuth."
hb_smtpConnect(smtpServer, smtpPort, fromEmail, token) // Authenticate with token
hb_smtpSend(fromEmail, toEmail, subject, body)
RETURN