Sample code for 30+ languages & platforms
AutoIt 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 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://www.googleapis.com/auth/gmail.imap_admin"
$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

;  With gmail.imap_admin, labels hidden by the user's Show in IMAP setting are included.
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()