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.
Chilkat PowerShell Downloads
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"
$success = $false
# Assumes Chilkat is unlocked. See Global Unlock Sample.
$mailbox = "user@example.com"
$key = New-Object Chilkat.JsonObject
$success = $key.LoadFile("gmail-service-account.json")
if ($success -ne $true) {
$("Could not load the private service-account key.")
exit
}
if (($key.StringOfEquals("type","service_account",$true) -ne $true) -or ($key.JsonTypeOf("private_key") -ne 1)) {
$("Expected a Google service-account JSON key.")
exit
}
$auth = New-Object Chilkat.AuthGoogle
$auth.JsonKey = $key.Emit()
$auth.SubEmailAddress = $mailbox
$auth.Scope = "https://www.googleapis.com/auth/gmail.imap_admin"
$auth.ExpireNumSeconds = 3600
$connection = New-Object Chilkat.Socket
$connection.RequireSslCertVerify = $true
$success = $connection.Connect("www.googleapis.com",443,$true,10000)
if ($success -ne $true) {
$("Could not establish the Google token connection.")
exit
}
$success = $auth.ObtainAccessToken($connection)
$connection.Close(1000)
if ($success -ne $true) {
$("Check the key, domain-wide delegation client ID/scope, user, and computer clock.")
exit
}
$imap = New-Object Chilkat.Imap
$imap.Ssl = $true
$imap.Port = 993
$imap.RequireSslCertVerify = $true
$imap.AuthMethod = "XOAUTH2"
$success = $imap.Connect("imap.gmail.com")
if ($success -ne $true) {
$("Could not establish the Gmail IMAP TLS connection.")
exit
}
$success = $imap.Login($mailbox,$auth.AccessToken)
if ($success -ne $true) {
$imap.Disconnect()
$("IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.")
exit
}
$folders = New-Object Chilkat.Mailboxes
$success = $imap.MbxList($false,"","*",$folders)
if ($success -ne $true) {
$imap.Logout()
$imap.Disconnect()
$("Could not list Gmail folders.")
exit
}
# With gmail.imap_admin, labels hidden by the user's Show in IMAP setting are included.
$i = 0
while (($i -lt $folders.Count)) {
$($folders.GetName($i))
$i = $i + 1
}
$success = $imap.ExamineMailbox("INBOX")
if ($success -ne $true) {
$imap.Logout()
$imap.Disconnect()
$("Could not examine the impersonated user's INBOX.")
exit
}
$("INBOX messages: " + $imap.NumMessages)
$imap.Logout()
$imap.Disconnect()