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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
String sMailbox
Handle hoKey
Handle hoAuth
Variant vConnection
Handle hoConnection
Handle hoImap
Variant vFolders
Handle hoFolders
Integer i
String sTemp1
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Move "user@example.com" To sMailbox
Get Create (RefClass(cComChilkatJsonObject)) To hoKey
If (Not(IsComObjectCreated(hoKey))) Begin
Send CreateComObject of hoKey
End
Get ComLoadFile Of hoKey "gmail-service-account.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not load the private service-account key."
Procedure_Return
End
Get ComStringOfEquals Of hoKey "type" "service_account" True To bTemp1
Get ComJsonTypeOf Of hoKey "private_key" To iTemp1
If ((bTemp1 <> True) Or (iTemp1 <> 1)) Begin
Showln "Expected a Google service-account JSON key."
Procedure_Return
End
Get Create (RefClass(cComChilkatAuthGoogle)) To hoAuth
If (Not(IsComObjectCreated(hoAuth))) Begin
Send CreateComObject of hoAuth
End
Get ComEmit Of hoKey To sTemp1
Set ComJsonKey Of hoAuth To sTemp1
Set ComSubEmailAddress Of hoAuth To sMailbox
Set ComScope Of hoAuth To "https://mail.google.com/"
Set ComExpireNumSeconds Of hoAuth To 3600
Get Create (RefClass(cComChilkatSocket)) To hoConnection
If (Not(IsComObjectCreated(hoConnection))) Begin
Send CreateComObject of hoConnection
End
Set ComRequireSslCertVerify Of hoConnection To True
Get ComConnect Of hoConnection "www.googleapis.com" 443 True 10000 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not establish the Google token connection."
Procedure_Return
End
Get pvComObject of hoConnection to vConnection
Get ComObtainAccessToken Of hoAuth vConnection To iSuccess
Get ComClose Of hoConnection 1000 To iSuccess
If (iSuccess <> True) Begin
Showln "Check the key, domain-wide delegation client ID/scope, user, and computer clock."
Procedure_Return
End
Get Create (RefClass(cComChilkatImap)) To hoImap
If (Not(IsComObjectCreated(hoImap))) Begin
Send CreateComObject of hoImap
End
Set ComSsl Of hoImap To True
Set ComPort Of hoImap To 993
Set ComRequireSslCertVerify Of hoImap To True
Set ComAuthMethod Of hoImap To "XOAUTH2"
Get ComConnect Of hoImap "imap.gmail.com" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not establish the Gmail IMAP TLS connection."
Procedure_Return
End
Get ComAccessToken Of hoAuth To sTemp1
Get ComLogin Of hoImap sMailbox sTemp1 To iSuccess
If (iSuccess <> True) Begin
Get ComDisconnect Of hoImap To iSuccess
Showln "IMAP authentication failed. Check the mailbox, scope, expiry, and account policy."
Procedure_Return
End
Get Create (RefClass(cComChilkatMailboxes)) To hoFolders
If (Not(IsComObjectCreated(hoFolders))) Begin
Send CreateComObject of hoFolders
End
Get pvComObject of hoFolders to vFolders
Get ComMbxList Of hoImap False "" "*" vFolders To iSuccess
If (iSuccess <> True) Begin
Get ComLogout Of hoImap To iSuccess
Get ComDisconnect Of hoImap To iSuccess
Showln "Could not list Gmail folders."
Procedure_Return
End
// List the labels visible through ordinary full-mail IMAP access.
Move 0 To i
While (i < (ComCount(hoFolders)))
Get ComGetName Of hoFolders i To sTemp1
Showln sTemp1
Move (i + 1) To i
Loop
Get ComExamineMailbox Of hoImap "INBOX" To iSuccess
If (iSuccess <> True) Begin
Get ComLogout Of hoImap To iSuccess
Get ComDisconnect Of hoImap To iSuccess
Showln "Could not examine the impersonated user's INBOX."
Procedure_Return
End
Get ComNumMessages Of hoImap To iTemp1
Showln "INBOX messages: " iTemp1
Get ComLogout Of hoImap To iSuccess
Get ComDisconnect Of hoImap To iSuccess
End_Procedure