Sample code for 30+ languages & platforms
Tcl 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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

set mailbox "user@example.com"
set key [new_CkJsonObject]

set success [CkJsonObject_LoadFile $key "gmail-service-account.json"]
if {$success != 1} then {
    puts "Could not load the private service-account key."
    delete_CkJsonObject $key
    exit
}

if {expr [[CkJsonObject_StringOfEquals $key "type" "service_account" 1] != 1]  ||  [[CkJsonObject_JsonTypeOf $key "private_key"] != 1]} then {
    puts "Expected a Google service-account JSON key."
    delete_CkJsonObject $key
    exit
}

set auth [new_CkAuthGoogle]

CkAuthGoogle_put_JsonKey $auth [CkJsonObject_emit $key]
CkAuthGoogle_put_SubEmailAddress $auth $mailbox
CkAuthGoogle_put_Scope $auth "https://www.googleapis.com/auth/gmail.imap_admin"
CkAuthGoogle_put_ExpireNumSeconds $auth 3600
set connection [new_CkSocket]

CkSocket_put_RequireSslCertVerify $connection 1
set success [CkSocket_Connect $connection "www.googleapis.com" 443 1 10000]
if {$success != 1} then {
    puts "Could not establish the Google token connection."
    delete_CkJsonObject $key
    delete_CkAuthGoogle $auth
    delete_CkSocket $connection
    exit
}

set success [CkAuthGoogle_ObtainAccessToken $auth $connection]
CkSocket_Close $connection 1000
if {$success != 1} then {
    puts "Check the key, domain-wide delegation client ID/scope, user, and computer clock."
    delete_CkJsonObject $key
    delete_CkAuthGoogle $auth
    delete_CkSocket $connection
    exit
}

set imap [new_CkImap]

CkImap_put_Ssl $imap 1
CkImap_put_Port $imap 993
CkImap_put_RequireSslCertVerify $imap 1
CkImap_put_AuthMethod $imap "XOAUTH2"
set success [CkImap_Connect $imap "imap.gmail.com"]
if {$success != 1} then {
    puts "Could not establish the Gmail IMAP TLS connection."
    delete_CkJsonObject $key
    delete_CkAuthGoogle $auth
    delete_CkSocket $connection
    delete_CkImap $imap
    exit
}

set success [CkImap_Login $imap $mailbox [CkAuthGoogle_accessToken $auth]]
if {$success != 1} then {
    CkImap_Disconnect $imap
    puts "IMAP authentication failed. Check the mailbox, scope, expiry, and account policy."
    delete_CkJsonObject $key
    delete_CkAuthGoogle $auth
    delete_CkSocket $connection
    delete_CkImap $imap
    exit
}

set folders [new_CkMailboxes]

set success [CkImap_MbxList $imap 0 "" "*" $folders]
if {$success != 1} then {
    CkImap_Logout $imap
    CkImap_Disconnect $imap
    puts "Could not list Gmail folders."
    delete_CkJsonObject $key
    delete_CkAuthGoogle $auth
    delete_CkSocket $connection
    delete_CkImap $imap
    delete_CkMailboxes $folders
    exit
}

#  With gmail.imap_admin, labels hidden by the user's Show in IMAP setting are included.
set i 0
while {$i < [CkMailboxes_get_Count $folders]} {
    puts [CkMailboxes_getName $folders $i]
    set i [expr $i + 1]
}
set success [CkImap_ExamineMailbox $imap "INBOX"]
if {$success != 1} then {
    CkImap_Logout $imap
    CkImap_Disconnect $imap
    puts "Could not examine the impersonated user's INBOX."
    delete_CkJsonObject $key
    delete_CkAuthGoogle $auth
    delete_CkSocket $connection
    delete_CkImap $imap
    delete_CkMailboxes $folders
    exit
}

puts "INBOX messages: [CkImap_get_NumMessages $imap]"
CkImap_Logout $imap
CkImap_Disconnect $imap

delete_CkJsonObject $key
delete_CkAuthGoogle $auth
delete_CkSocket $connection
delete_CkImap $imap
delete_CkMailboxes $folders