Gmail IMAP: Workspace Service Account with a JSON Key
Use a service-account JSON key with Google Workspace domain-wide delegation. A Workspace administrator must authorize the service account's numeric OAuth client ID for https://mail.google.com/. Set SubEmailAddress to the Workspace user whose active Gmail mailbox is being accessed, and use that same address for IMAP login.
A service account has no Gmail mailbox of its own; leaving the impersonated user empty does not grant access to a personal Gmail account. The signed JWT exchange yields a short-lived token without a refresh token. Obtain another when needed, protect the private key, and keep the computer clock accurate.
This example lists visible folders and examines INBOX read-only. For the different visibility rules of the IMAP-only administrative scope, see gmail.imap_admin delegation.
See the Gmail SMTP, IMAP, and POP3 guide for current authentication choices and Gmail-specific examples.
Chilkat Visual FoxPro Downloads
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://mail.google.com/"
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
* List the labels visible through ordinary full-mail IMAP access.
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