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

PowerBuilder
integer li_rc
long li_Success
string ls_Mailbox
oleobject loo_Key
oleobject loo_Auth
oleobject loo_Connection
oleobject loo_Imap
oleobject loo_Folders
long i

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

ls_Mailbox = "user@example.com"
loo_Key = create oleobject
li_rc = loo_Key.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
    destroy loo_Key
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Key.LoadFile("gmail-service-account.json")
if li_Success <> 1 then
    Write-Debug "Could not load the private service-account key."
    destroy loo_Key
    return
end if

if (loo_Key.StringOfEquals("type","service_account",1) <> 1) OR (loo_Key.JsonTypeOf("private_key") <> 1) then
    Write-Debug "Expected a Google service-account JSON key."
    destroy loo_Key
    return
end if

loo_Auth = create oleobject
li_rc = loo_Auth.ConnectToNewObject("Chilkat.AuthGoogle")

loo_Auth.JsonKey = loo_Key.Emit()
loo_Auth.SubEmailAddress = ls_Mailbox
loo_Auth.Scope = "https://www.googleapis.com/auth/gmail.imap_admin"
loo_Auth.ExpireNumSeconds = 3600
loo_Connection = create oleobject
li_rc = loo_Connection.ConnectToNewObject("Chilkat.Socket")

loo_Connection.RequireSslCertVerify = 1
li_Success = loo_Connection.Connect("www.googleapis.com",443,1,10000)
if li_Success <> 1 then
    Write-Debug "Could not establish the Google token connection."
    destroy loo_Key
    destroy loo_Auth
    destroy loo_Connection
    return
end if

li_Success = loo_Auth.ObtainAccessToken(loo_Connection)
loo_Connection.Close(1000)
if li_Success <> 1 then
    Write-Debug "Check the key, domain-wide delegation client ID/scope, user, and computer clock."
    destroy loo_Key
    destroy loo_Auth
    destroy loo_Connection
    return
end if

loo_Imap = create oleobject
li_rc = loo_Imap.ConnectToNewObject("Chilkat.Imap")

loo_Imap.Ssl = 1
loo_Imap.Port = 993
loo_Imap.RequireSslCertVerify = 1
loo_Imap.AuthMethod = "XOAUTH2"
li_Success = loo_Imap.Connect("imap.gmail.com")
if li_Success <> 1 then
    Write-Debug "Could not establish the Gmail IMAP TLS connection."
    destroy loo_Key
    destroy loo_Auth
    destroy loo_Connection
    destroy loo_Imap
    return
end if

li_Success = loo_Imap.Login(ls_Mailbox,loo_Auth.AccessToken)
if li_Success <> 1 then
    loo_Imap.Disconnect()
    Write-Debug "IMAP authentication failed. Check the mailbox, scope, expiry, and account policy."
    destroy loo_Key
    destroy loo_Auth
    destroy loo_Connection
    destroy loo_Imap
    return
end if

loo_Folders = create oleobject
li_rc = loo_Folders.ConnectToNewObject("Chilkat.Mailboxes")

li_Success = loo_Imap.MbxList(0,"","*",loo_Folders)
if li_Success <> 1 then
    loo_Imap.Logout()
    loo_Imap.Disconnect()
    Write-Debug "Could not list Gmail folders."
    destroy loo_Key
    destroy loo_Auth
    destroy loo_Connection
    destroy loo_Imap
    destroy loo_Folders
    return
end if

//  With gmail.imap_admin, labels hidden by the user's Show in IMAP setting are included.
i = 0
do while (i < loo_Folders.Count)
    Write-Debug loo_Folders.GetName(i)
    i = i + 1
loop
li_Success = loo_Imap.ExamineMailbox("INBOX")
if li_Success <> 1 then
    loo_Imap.Logout()
    loo_Imap.Disconnect()
    Write-Debug "Could not examine the impersonated user's INBOX."
    destroy loo_Key
    destroy loo_Auth
    destroy loo_Connection
    destroy loo_Imap
    destroy loo_Folders
    return
end if

Write-Debug "INBOX messages: " + string(loo_Imap.NumMessages)
loo_Imap.Logout()
loo_Imap.Disconnect()


destroy loo_Key
destroy loo_Auth
destroy loo_Connection
destroy loo_Imap
destroy loo_Folders