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

Gmail IMAP: Workspace Domain-Wide Delegation

See more GMail SMTP/IMAP/POP Examples

Use a JSON service-account key to impersonate a Workspace user for IMAP with https://www.googleapis.com/auth/gmail.imap_admin. A Workspace administrator must authorize the service account's numeric OAuth client ID for this exact scope in domain-wide delegation. The impersonated user must have an active Gmail mailbox.

This IMAP-only scope reveals labels hidden by Show in IMAP and messages excluded by the user's folder-size limit. It does not authorize SMTP or POP and is not available for personal Gmail accounts or ordinary user-consent flows. Workspace administrative access policies still apply.

Protect the private key and limit who can execute the delegated application. The signed JWT exchange produces a short-lived token without a refresh token; obtain another when necessary and reconnect. Keep the computer clock accurate. This example lists folders and examines INBOX read-only without printing credentials.

Google documentation

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL lcMailbox
LOCAL loKey
LOCAL loAuth
LOCAL loConnection
LOCAL loImap
LOCAL loFolders
LOCAL i

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.

lcMailbox = "user@example.com"
loKey = CreateObject('Chilkat.JsonObject')
lnSuccess = loKey.LoadFile("gmail-service-account.json")
IF (lnSuccess <> 1) THEN
    ? "Could not load the private service-account key."
    RELEASE loKey
    CANCEL
ENDIF

IF ((loKey.StringOfEquals("type","service_account",1) <> 1) OR (loKey.JsonTypeOf("private_key") <> 1)) THEN
    ? "Expected a Google service-account JSON key."
    RELEASE loKey
    CANCEL
ENDIF

loAuth = CreateObject('Chilkat.AuthGoogle')
loAuth.JsonKey = loKey.Emit()
loAuth.SubEmailAddress = lcMailbox
loAuth.Scope = "https://www.googleapis.com/auth/gmail.imap_admin"
loAuth.ExpireNumSeconds = 3600
loConnection = CreateObject('Chilkat.Socket')
loConnection.RequireSslCertVerify = 1
lnSuccess = loConnection.Connect("www.googleapis.com",443,1,10000)
IF (lnSuccess <> 1) THEN
    ? "Could not establish the Google token connection."
    RELEASE loKey
    RELEASE loAuth
    RELEASE loConnection
    CANCEL
ENDIF

lnSuccess = loAuth.ObtainAccessToken(loConnection)
loConnection.Close(1000)
IF (lnSuccess <> 1) THEN
    ? "Check the key, domain-wide delegation client ID/scope, user, and computer clock."
    RELEASE loKey
    RELEASE loAuth
    RELEASE loConnection
    CANCEL
ENDIF

loImap = CreateObject('Chilkat.Imap')
loImap.Ssl = 1
loImap.Port = 993
loImap.RequireSslCertVerify = 1
loImap.AuthMethod = "XOAUTH2"
lnSuccess = loImap.Connect("imap.gmail.com")
IF (lnSuccess <> 1) THEN
    ? "Could not establish the Gmail IMAP TLS connection."
    RELEASE loKey
    RELEASE loAuth
    RELEASE loConnection
    RELEASE loImap
    CANCEL
ENDIF

lnSuccess = loImap.Login(lcMailbox,loAuth.AccessToken)
IF (lnSuccess <> 1) THEN
    loImap.Disconnect()
    ? "IMAP authentication failed. Check the mailbox, scope, expiry, and account policy."
    RELEASE loKey
    RELEASE loAuth
    RELEASE loConnection
    RELEASE loImap
    CANCEL
ENDIF

loFolders = CreateObject('Chilkat.Mailboxes')
lnSuccess = loImap.MbxList(0,"","*",loFolders)
IF (lnSuccess <> 1) THEN
    loImap.Logout()
    loImap.Disconnect()
    ? "Could not list Gmail folders."
    RELEASE loKey
    RELEASE loAuth
    RELEASE loConnection
    RELEASE loImap
    RELEASE loFolders
    CANCEL
ENDIF

*  With gmail.imap_admin, labels hidden by the user's Show in IMAP setting are included.
i = 0
DO WHILE (i < loFolders.Count)
    ? loFolders.GetName(i)
    i = i + 1
ENDDO
lnSuccess = loImap.ExamineMailbox("INBOX")
IF (lnSuccess <> 1) THEN
    loImap.Logout()
    loImap.Disconnect()
    ? "Could not examine the impersonated user's INBOX."
    RELEASE loKey
    RELEASE loAuth
    RELEASE loConnection
    RELEASE loImap
    RELEASE loFolders
    CANCEL
ENDIF

? "INBOX messages: " + STR(loImap.NumMessages)
loImap.Logout()
loImap.Disconnect()

RELEASE loKey
RELEASE loAuth
RELEASE loConnection
RELEASE loImap
RELEASE loFolders