Authorize a desktop application to use Gmail
See more GMail REST API Examples
Enable the Gmail API, configure the OAuth consent screen, and create a Desktop app OAuth client. Launch the system browser and receive the authorization code on a loopback listener. The sample requests read-only access with PKCE and offline access. For another operation, select the scope in that example before authorizing. Gmail scopes.
Run in a private, user-only directory. The file contains bearer credentials. In a deployed application use protected credential storage. To reuse an existing grant, copy the older examples’ qa_data/tokens/gmail.json to gmail-token.json in this private directory. The token retains its original scopes. Gmail clients using IMAP, POP, or SMTP with OAuth need the https://mail.google.com/ scope. Use a separate cache for each user and client.
Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Oauth
string ls_AuthUrl
long li_Waited
oleobject loo_Token
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
loo_Oauth = create oleobject
li_rc = loo_Oauth.ConnectToNewObject("Chilkat.OAuth2")
if li_rc < 0 then
destroy loo_Oauth
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Oauth.TokenEndpoint = "https://oauth2.googleapis.com/token"
loo_Oauth.ClientId = "YOUR_DESKTOP_CLIENT_ID"
loo_Oauth.ClientSecret = "YOUR_DESKTOP_CLIENT_SECRET"
loo_Oauth.AuthorizationEndpoint = "https://accounts.google.com/o/oauth2/v2/auth"
loo_Oauth.LocalHost = "127.0.0.1"
loo_Oauth.ListenPort = 55568
loo_Oauth.ListenPortRangeEnd = 55578
loo_Oauth.CodeChallenge = 1
loo_Oauth.CodeChallengeMethod = "S256"
loo_Oauth.Scope = "https://www.googleapis.com/auth/gmail.readonly"
li_Success = loo_Oauth.AddAuthQueryParam("access_type","offline")
if li_Success <> 1 then
Write-Debug "Could not configure offline access."
destroy loo_Oauth
return
end if
// This is the interactive setup step. Subsequent refreshes use the saved refresh token.
li_Success = loo_Oauth.AddAuthQueryParam("prompt","consent")
if li_Success <> 1 then
Write-Debug "Could not configure consent."
destroy loo_Oauth
return
end if
ls_AuthUrl = loo_Oauth.StartAuth()
if loo_Oauth.LastMethodSuccess <> 1 then
Write-Debug "Could not start the loopback listener."
destroy loo_Oauth
return
end if
li_Success = loo_Oauth.LaunchBrowser(ls_AuthUrl)
if li_Success <> 1 then
loo_Oauth.Cancel()
Write-Debug "Could not open the system browser."
destroy loo_Oauth
return
end if
li_Waited = 0
do while ((loo_Oauth.AuthFlowState < 3) AND (li_Waited < 180000))
loo_Oauth.SleepMs(200)
li_Waited = li_Waited + 200
loop
if loo_Oauth.AuthFlowState < 3 then
loo_Oauth.Cancel()
Write-Debug "Authorization timed out."
destroy loo_Oauth
return
end if
if loo_Oauth.AuthFlowState <> 3 then
Write-Debug "Authorization did not complete. Flow state: " + string(loo_Oauth.AuthFlowState)
destroy loo_Oauth
return
end if
loo_Token = create oleobject
li_rc = loo_Token.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Token.Load(loo_Oauth.AccessTokenResponse)
if li_Success <> 1 then
Write-Debug "Could not parse the token response."
destroy loo_Oauth
destroy loo_Token
return
end if
if (loo_Token.JsonTypeOf("refresh_token") <> 1) OR (loo_Token.StringOfEquals("refresh_token","",1) = 1) then
Write-Debug "No offline refresh token was issued. The existing cache was preserved."
destroy loo_Oauth
destroy loo_Token
return
end if
li_Success = loo_Token.WriteFile("gmail-token.json")
if li_Success <> 1 then
Write-Debug "Could not save the token file."
destroy loo_Oauth
destroy loo_Token
return
end if
Write-Debug "Credentials saved in the private working directory."
destroy loo_Oauth
destroy loo_Token