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

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.

Reference documentation

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

;  Assumes Chilkat is unlocked. See Global Unlock Sample.

Local $sMailbox = "user@example.com"
$oKey = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oKey.LoadFile("gmail-service-account.json")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not load the private service-account key." & @CRLF)
    Exit
EndIf

If (($oKey.StringOfEquals("type","service_account",True) <> True) Or ($oKey.JsonTypeOf("private_key") <> 1)) Then
    ConsoleWrite("Expected a Google service-account JSON key." & @CRLF)
    Exit
EndIf

$oAuth = ObjCreate("Chilkat.AuthGoogle")
$oAuth.JsonKey = $oKey.Emit()
$oAuth.SubEmailAddress = $sMailbox
$oAuth.Scope = "https://mail.google.com/"
$oAuth.ExpireNumSeconds = 3600
$oConnection = ObjCreate("Chilkat.Socket")
$oConnection.RequireSslCertVerify = True
$bSuccess = $oConnection.Connect("www.googleapis.com",443,True,10000)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not establish the Google token connection." & @CRLF)
    Exit
EndIf

$bSuccess = $oAuth.ObtainAccessToken($oConnection)
$oConnection.Close(1000)
If ($bSuccess <> True) Then
    ConsoleWrite("Check the key, domain-wide delegation client ID/scope, user, and computer clock." & @CRLF)
    Exit
EndIf

$oImap = ObjCreate("Chilkat.Imap")
$oImap.Ssl = True
$oImap.Port = 993
$oImap.RequireSslCertVerify = True
$oImap.AuthMethod = "XOAUTH2"
$bSuccess = $oImap.Connect("imap.gmail.com")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not establish the Gmail IMAP TLS connection." & @CRLF)
    Exit
EndIf

$bSuccess = $oImap.Login($sMailbox,$oAuth.AccessToken)
If ($bSuccess <> True) Then
    $oImap.Disconnect()
    ConsoleWrite("IMAP authentication failed. Check the mailbox, scope, expiry, and account policy." & @CRLF)
    Exit
EndIf

$oFolders = ObjCreate("Chilkat.Mailboxes")
$bSuccess = $oImap.MbxList(False,"","*",$oFolders)
If ($bSuccess <> True) Then
    $oImap.Logout()
    $oImap.Disconnect()
    ConsoleWrite("Could not list Gmail folders." & @CRLF)
    Exit
EndIf

;  List the labels visible through ordinary full-mail IMAP access.
Local $i = 0
While ($i < $oFolders.Count)
    ConsoleWrite($oFolders.GetName($i) & @CRLF)
    $i = $i + 1
Wend
$bSuccess = $oImap.ExamineMailbox("INBOX")
If ($bSuccess <> True) Then
    $oImap.Logout()
    $oImap.Disconnect()
    ConsoleWrite("Could not examine the impersonated user's INBOX." & @CRLF)
    Exit
EndIf

ConsoleWrite("INBOX messages: " & $oImap.NumMessages & @CRLF)
$oImap.Logout()
$oImap.Disconnect()