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

Check Gmail from a scheduled job after one-time consent

See more GMail REST API Examples

First run desktop authorization with gmail.readonly and offline access. This applies to personal Gmail and Workspace accounts subject to organization policies. Configure a job to run this code using the same private working directory and user identity. Each run refreshes the token and displays up to ten inbox message headers.

Save the original OAuth client credentials in a private gmail-client.json containing client_id and client_secret. The existing gmail-token.json supplies the refresh token. Use protected credential storage in a deployed application and serialize runs sharing these files. An invalid_grant response requires reconnecting the account. External OAuth apps in Testing normally receive refresh tokens that expire after seven days when Gmail scopes are requested. Google token expiration guidance.

This is a repeatable inbox check. To process each mailbox change across runs, see mailbox synchronization.

Google documentation

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loFiles
LOCAL lnFileStatus
LOCAL lnBackupStatus
LOCAL loToken
LOCAL loClient
LOCAL loHttp
LOCAL loForm
LOCAL loResp
LOCAL loReply
LOCAL loIds
LOCAL i
LOCAL loUrl
LOCAL h

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.

loFiles = CreateObject('Chilkat.FileAccess')
*  Recover the previous complete file if a run stopped during replacement.
lnFileStatus = loFiles.FileExists3("gmail-token.json")
IF (lnFileStatus < 0) THEN
    ? "Could not inspect gmail-token.json."
    RELEASE loFiles
    CANCEL
ENDIF

IF (lnFileStatus = 0) THEN
    lnBackupStatus = loFiles.FileExists3("gmail-token.json.previous")
    IF (lnBackupStatus < 0) THEN
        ? "Could not inspect the backup file."
        RELEASE loFiles
        CANCEL
    ENDIF

    IF (lnBackupStatus = 1) THEN
        lnSuccess = loFiles.FileRename("gmail-token.json.previous","gmail-token.json")
        IF (lnSuccess <> 1) THEN
            ? "Could not restore the previous complete file."
            RELEASE loFiles
            CANCEL
        ENDIF

        lnFileStatus = 1
    ENDIF

ENDIF

loToken = CreateObject('Chilkat.JsonObject')
lnSuccess = loToken.LoadFile("gmail-token.json")
IF (lnSuccess <> 1) THEN
    ? "Could not read gmail-token.json from the private working directory."
    RELEASE loFiles
    RELEASE loToken
    CANCEL
ENDIF

IF ((loToken.JsonTypeOf("refresh_token") <> 1) OR (loToken.StringOfEquals("refresh_token","",1) = 1)) THEN
    ? "Authorize this account for offline access first."
    RELEASE loFiles
    RELEASE loToken
    CANCEL
ENDIF

loClient = CreateObject('Chilkat.JsonObject')
lnSuccess = loClient.LoadFile("gmail-client.json")
IF (lnSuccess <> 1) THEN
    ? "Could not load the private OAuth client file."
    RELEASE loFiles
    RELEASE loToken
    RELEASE loClient
    CANCEL
ENDIF

IF ((loClient.JsonTypeOf("client_id") <> 1) OR (loClient.JsonTypeOf("client_secret") <> 1)) THEN
    ? "gmail-client.json must contain client_id and client_secret."
    RELEASE loFiles
    RELEASE loToken
    RELEASE loClient
    CANCEL
ENDIF

loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
loForm = CreateObject('Chilkat.HttpRequest')
loForm.HttpVerb = "POST"
loForm.ContentType = "application/x-www-form-urlencoded"
loForm.AddParam("grant_type","refresh_token")
loForm.AddParam("client_id",loClient.StringOf("client_id"))
loForm.AddParam("client_secret",loClient.StringOf("client_secret"))
loForm.AddParam("refresh_token",loToken.StringOf("refresh_token"))
loResp = CreateObject('Chilkat.HttpResponse')
loReply = CreateObject('Chilkat.JsonObject')
lnSuccess = loHttp.HttpReq("https://oauth2.googleapis.com/token",loForm,loResp)
IF (loResp.StatusCode = 0) THEN
    ? "The token endpoint did not return an HTTP response."
    RELEASE loFiles
    RELEASE loToken
    RELEASE loClient
    RELEASE loHttp
    RELEASE loForm
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

IF ((lnSuccess <> 1) OR (loResp.StatusCode <> 200)) THEN
    ? "Token refresh failed. HTTP " + STR(loResp.StatusCode)
    lnSuccess = loReply.Load(loResp.BodyStr)
    IF (lnSuccess = 1) THEN
        IF (loReply.StringOfEquals("error","invalid_grant",1) = 1) THEN
            ? "Reconnect this account with interactive authorization."
        ENDIF

    ENDIF

    RELEASE loFiles
    RELEASE loToken
    RELEASE loClient
    RELEASE loHttp
    RELEASE loForm
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

lnSuccess = loReply.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
    ? "Could not parse the refresh response."
    RELEASE loFiles
    RELEASE loToken
    RELEASE loClient
    RELEASE loHttp
    RELEASE loForm
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

IF ((loReply.JsonTypeOf("access_token") <> 1) OR (loReply.StringOfEquals("access_token","",1) = 1)) THEN
    ? "Expected access_token in the response."
    RELEASE loFiles
    RELEASE loToken
    RELEASE loClient
    RELEASE loHttp
    RELEASE loForm
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

*  Google usually omits the refresh token. Preserve it unless a replacement is returned.
IF ((loReply.JsonTypeOf("refresh_token") <> 1) OR (loReply.StringOfEquals("refresh_token","",1) = 1)) THEN
    lnSuccess = loReply.UpdateString("refresh_token",loToken.StringOf("refresh_token"))
    IF (lnSuccess <> 1) THEN
        ? loReply.LastErrorText
        RELEASE loFiles
        RELEASE loToken
        RELEASE loClient
        RELEASE loHttp
        RELEASE loForm
        RELEASE loResp
        RELEASE loReply
        CANCEL
    ENDIF

ENDIF

IF ((loReply.HasMember("scope") <> 1) AND (loToken.JsonTypeOf("scope") = 1)) THEN
    lnSuccess = loReply.UpdateString("scope",loToken.StringOf("scope"))
    IF (lnSuccess <> 1) THEN
        ? loReply.LastErrorText
        RELEASE loFiles
        RELEASE loToken
        RELEASE loClient
        RELEASE loHttp
        RELEASE loForm
        RELEASE loResp
        RELEASE loReply
        CANCEL
    ENDIF

ENDIF

*  Prepare the complete replacement before moving the last committed file.
lnSuccess = loReply.WriteFile("gmail-token.json.new")
IF (lnSuccess <> 1) THEN
    ? "Could not write the replacement file."
    RELEASE loFiles
    RELEASE loToken
    RELEASE loClient
    RELEASE loHttp
    RELEASE loForm
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

IF (loFiles.FileExists("gmail-token.json") = 1) THEN
    IF (loFiles.FileExists("gmail-token.json.previous") = 1) THEN
        lnSuccess = loFiles.FileDelete("gmail-token.json.previous")
        IF (lnSuccess <> 1) THEN
            ? "Could not rotate the previous backup."
            RELEASE loFiles
            RELEASE loToken
            RELEASE loClient
            RELEASE loHttp
            RELEASE loForm
            RELEASE loResp
            RELEASE loReply
            CANCEL
        ENDIF

    ENDIF

    lnSuccess = loFiles.FileRename("gmail-token.json","gmail-token.json.previous")
    IF (lnSuccess <> 1) THEN
        ? "Could not retain the previous complete file."
        RELEASE loFiles
        RELEASE loToken
        RELEASE loClient
        RELEASE loHttp
        RELEASE loForm
        RELEASE loResp
        RELEASE loReply
        CANCEL
    ENDIF

ENDIF

lnSuccess = loFiles.FileRename("gmail-token.json.new","gmail-token.json")
IF (lnSuccess <> 1) THEN
    ? "Could not commit the replacement. The previous complete file is retained."
    RELEASE loFiles
    RELEASE loToken
    RELEASE loClient
    RELEASE loHttp
    RELEASE loForm
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

loHttp.AuthToken = loReply.StringOf("access_token")
lnSuccess = loHttp.HttpNoBody("GET","https://gmail.googleapis.com/gmail/v1/users/me/messages?labelIds=INBOX&maxResults=10",loResp)
IF (loResp.StatusCode = 0) THEN
    ? "Gmail did not return an HTTP response."
    RELEASE loFiles
    RELEASE loToken
    RELEASE loClient
    RELEASE loHttp
    RELEASE loForm
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

IF ((lnSuccess <> 1) OR (loResp.StatusCode < 200) OR (loResp.StatusCode >= 300)) THEN
    ? "Gmail HTTP " + STR(loResp.StatusCode)
    lnSuccess = loReply.Load(loResp.BodyStr)
    IF (lnSuccess = 1) THEN
        ? loReply.StringOf("error.message")
    ENDIF

    RELEASE loFiles
    RELEASE loToken
    RELEASE loClient
    RELEASE loHttp
    RELEASE loForm
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

lnSuccess = loReply.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
    ? "Expected a Gmail JSON response."
    RELEASE loFiles
    RELEASE loToken
    RELEASE loClient
    RELEASE loHttp
    RELEASE loForm
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

loIds = CreateObject('Chilkat.StringTable')
i = 0
DO WHILE (i < loReply.SizeOfArray("messages"))
    loReply.I = i
    lnSuccess = loIds.Append(loReply.StringOf("messages[i].id"))
    IF (lnSuccess <> 1) THEN
        ? loIds.LastErrorText
        RELEASE loFiles
        RELEASE loToken
        RELEASE loClient
        RELEASE loHttp
        RELEASE loForm
        RELEASE loResp
        RELEASE loReply
        RELEASE loIds
        CANCEL
    ENDIF

    i = i + 1
ENDDO
loUrl = CreateObject('Chilkat.StringBuilder')
i = 0
DO WHILE (i < loIds.Count)
    loUrl.SetString("https://gmail.googleapis.com/gmail/v1/users/me/messages/")
    loUrl.Append(loHttp.UrlEncode(loIds.StringAt(i)))
    loUrl.Append("?format=metadata&metadataHeaders=From&metadataHeaders=Subject")
    lnSuccess = loHttp.HttpNoBody("GET",loUrl.GetAsString(),loResp)
    IF (loResp.StatusCode = 0) THEN
        ? "Gmail did not return an HTTP response."
        RELEASE loFiles
        RELEASE loToken
        RELEASE loClient
        RELEASE loHttp
        RELEASE loForm
        RELEASE loResp
        RELEASE loReply
        RELEASE loIds
        RELEASE loUrl
        CANCEL
    ENDIF

    IF ((lnSuccess <> 1) OR (loResp.StatusCode < 200) OR (loResp.StatusCode >= 300)) THEN
        ? "Gmail HTTP " + STR(loResp.StatusCode)
        lnSuccess = loReply.Load(loResp.BodyStr)
        IF (lnSuccess = 1) THEN
            ? loReply.StringOf("error.message")
        ENDIF

        RELEASE loFiles
        RELEASE loToken
        RELEASE loClient
        RELEASE loHttp
        RELEASE loForm
        RELEASE loResp
        RELEASE loReply
        RELEASE loIds
        RELEASE loUrl
        CANCEL
    ENDIF

    lnSuccess = loReply.Load(loResp.BodyStr)
    IF (lnSuccess <> 1) THEN
        ? "Expected a Gmail JSON response."
        RELEASE loFiles
        RELEASE loToken
        RELEASE loClient
        RELEASE loHttp
        RELEASE loForm
        RELEASE loResp
        RELEASE loReply
        RELEASE loIds
        RELEASE loUrl
        CANCEL
    ENDIF

    ? "Message: " + loIds.StringAt(i)
    h = 0
    DO WHILE (h < loReply.SizeOfArray("payload.headers"))
        loReply.I = h
        ? loReply.StringOf("payload.headers[i].name") + ": " + loReply.StringOf("payload.headers[i].value")
        h = h + 1
    ENDDO
    i = i + 1
ENDDO
? "Inbox messages checked: " + STR(loIds.Count)

RELEASE loFiles
RELEASE loToken
RELEASE loClient
RELEASE loHttp
RELEASE loForm
RELEASE loResp
RELEASE loReply
RELEASE loIds
RELEASE loUrl