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
FUNCTION RequestAccessToken(tokenUrl, clientId, clientSecret, authorizationCode)
LOCAL postData, response, jsonResponse, accessToken := ""
// Prepare the POST data as a plain string
postData := "code=" + authorizationCode + "&" + ;
"client_id=" + clientId + "&" + ;
"client_secret=" + clientSecret + "&" + ;
"redirect_uri=urn:ietf:wg:oauth:2.0:oob&" + ;
"grant_type=authorization_code"
// Make a POST request to the token URL
response := hb_curlPost(tokenUrl, postData, { "Content-Type: application/x-www-form-urlencoded" })
IF !Empty(response)
// Parse the JSON response to extract the access token
jsonResponse := hb_jsonDecode(response)
IF hb_IsObject(jsonResponse)
accessToken := hb_jsonGet(jsonResponse, "access_token")
ELSE
? "Error parsing JSON response."
ENDIF
ELSE
? "Error: No response from the token endpoint."
ENDIF
RETURN accessToken
Return to FiveWin for Harbour/xHarbour
Users browsing this forum: No registered users and 19 guests