Sample code for 30+ languages & platforms
Visual FoxPro Requires Chilkat v11.0.0+

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.

Google documentation

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loOauth
LOCAL lcAuthUrl
LOCAL lnWaited
LOCAL loToken

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.

loOauth = CreateObject('Chilkat.OAuth2')
loOauth.TokenEndpoint = "https://oauth2.googleapis.com/token"
loOauth.ClientId = "YOUR_DESKTOP_CLIENT_ID"
loOauth.ClientSecret = "YOUR_DESKTOP_CLIENT_SECRET"

loOauth.AuthorizationEndpoint = "https://accounts.google.com/o/oauth2/v2/auth"
loOauth.LocalHost = "127.0.0.1"
loOauth.ListenPort = 55568
loOauth.ListenPortRangeEnd = 55578
loOauth.CodeChallenge = 1
loOauth.CodeChallengeMethod = "S256"
loOauth.Scope = "https://www.googleapis.com/auth/gmail.readonly"
lnSuccess = loOauth.AddAuthQueryParam("access_type","offline")
IF (lnSuccess <> 1) THEN
    ? "Could not configure offline access."
    RELEASE loOauth
    CANCEL
ENDIF

*  This is the interactive setup step. Subsequent refreshes use the saved refresh token.
lnSuccess = loOauth.AddAuthQueryParam("prompt","consent")
IF (lnSuccess <> 1) THEN
    ? "Could not configure consent."
    RELEASE loOauth
    CANCEL
ENDIF

lcAuthUrl = loOauth.StartAuth()
IF (loOauth.LastMethodSuccess <> 1) THEN
    ? "Could not start the loopback listener."
    RELEASE loOauth
    CANCEL
ENDIF

lnSuccess = loOauth.LaunchBrowser(lcAuthUrl)
IF (lnSuccess <> 1) THEN
    loOauth.Cancel()
    ? "Could not open the system browser."
    RELEASE loOauth
    CANCEL
ENDIF

lnWaited = 0
DO WHILE ((loOauth.AuthFlowState < 3) AND (lnWaited < 180000))
    loOauth.SleepMs(200)
    lnWaited = lnWaited + 200
ENDDO
IF (loOauth.AuthFlowState < 3) THEN
    loOauth.Cancel()
    ? "Authorization timed out."
    RELEASE loOauth
    CANCEL
ENDIF

IF (loOauth.AuthFlowState <> 3) THEN
    ? "Authorization did not complete. Flow state: " + STR(loOauth.AuthFlowState)
    RELEASE loOauth
    CANCEL
ENDIF

loToken = CreateObject('Chilkat.JsonObject')
lnSuccess = loToken.Load(loOauth.AccessTokenResponse)
IF (lnSuccess <> 1) THEN
    ? "Could not parse the token response."
    RELEASE loOauth
    RELEASE loToken
    CANCEL
ENDIF

IF ((loToken.JsonTypeOf("refresh_token") <> 1) OR (loToken.StringOfEquals("refresh_token","",1) = 1)) THEN
    ? "No offline refresh token was issued. The existing cache was preserved."
    RELEASE loOauth
    RELEASE loToken
    CANCEL
ENDIF

lnSuccess = loToken.WriteFile("gmail-token.json")
IF (lnSuccess <> 1) THEN
    ? "Could not save the token file."
    RELEASE loOauth
    RELEASE loToken
    CANCEL
ENDIF

? "Credentials saved in the private working directory."

RELEASE loOauth
RELEASE loToken