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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oOauth
LOCAL cAuthUrl
LOCAL nWaited
LOCAL oToken
nSuccess := 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
oOauth := CreateObject("Chilkat.OAuth2")
oOauth:TokenEndpoint := "https://oauth2.googleapis.com/token"
oOauth:ClientId := "YOUR_DESKTOP_CLIENT_ID"
oOauth:ClientSecret := "YOUR_DESKTOP_CLIENT_SECRET"
oOauth:AuthorizationEndpoint := "https://accounts.google.com/o/oauth2/v2/auth"
oOauth:LocalHost := "127.0.0.1"
oOauth:ListenPort := 55568
oOauth:ListenPortRangeEnd := 55578
oOauth:CodeChallenge := 1
oOauth:CodeChallengeMethod := "S256"
oOauth:Scope := "https://www.googleapis.com/auth/gmail.readonly"
nSuccess := oOauth:AddAuthQueryParam("access_type", "offline")
IF (nSuccess != 1)
? "Could not configure offline access."
oOauth:destroy()
RETURN
ENDIF
// This is the interactive setup step. Subsequent refreshes use the saved refresh token.
nSuccess := oOauth:AddAuthQueryParam("prompt", "consent")
IF (nSuccess != 1)
? "Could not configure consent."
oOauth:destroy()
RETURN
ENDIF
cAuthUrl := oOauth:StartAuth()
IF (oOauth:LastMethodSuccess != 1)
? "Could not start the loopback listener."
oOauth:destroy()
RETURN
ENDIF
nSuccess := oOauth:LaunchBrowser(cAuthUrl)
IF (nSuccess != 1)
oOauth:Cancel()
? "Could not open the system browser."
oOauth:destroy()
RETURN
ENDIF
nWaited := 0
DO WHILE ((oOauth:AuthFlowState < 3) .AND. (nWaited < 180000))
oOauth:SleepMs(200)
nWaited := nWaited + 200
ENDDO
IF (oOauth:AuthFlowState < 3)
oOauth:Cancel()
? "Authorization timed out."
oOauth:destroy()
RETURN
ENDIF
IF (oOauth:AuthFlowState != 3)
? "Authorization did not complete. Flow state: " + Str(oOauth:AuthFlowState)
oOauth:destroy()
RETURN
ENDIF
oToken := CreateObject("Chilkat.JsonObject")
nSuccess := oToken:Load(oOauth:AccessTokenResponse)
IF (nSuccess != 1)
? "Could not parse the token response."
oOauth:destroy()
oToken:destroy()
RETURN
ENDIF
IF ((oToken:JsonTypeOf("refresh_token") != 1) .OR. (oToken:StringOfEquals("refresh_token", "", 1) == 1))
? "No offline refresh token was issued. The existing cache was preserved."
oOauth:destroy()
oToken:destroy()
RETURN
ENDIF
nSuccess := oToken:WriteFile("gmail-token.json")
IF (nSuccess != 1)
? "Could not save the token file."
oOauth:destroy()
oToken:destroy()
RETURN
ENDIF
? "Credentials saved in the private working directory."
oOauth:destroy()
oToken:destroy()