Sample code for 30+ languages & platforms
PowerBuilder 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 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://mail.google.com/"
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

//  List the labels visible through ordinary full-mail IMAP access.
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