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

PureBasic
IncludeFile "CkSocket.pb"
IncludeFile "CkAuthGoogle.pb"
IncludeFile "CkMailboxes.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkImap.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.

    mailbox.s = "user@example.com"
    key.i = CkJsonObject::ckCreate()
    If key.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkJsonObject::ckLoadFile(key,"gmail-service-account.json")
    If success <> 1
        Debug "Could not load the private service-account key."
        CkJsonObject::ckDispose(key)
        ProcedureReturn
    EndIf

    If (CkJsonObject::ckStringOfEquals(key,"type","service_account",1) <> 1) OR (CkJsonObject::ckJsonTypeOf(key,"private_key") <> 1)
        Debug "Expected a Google service-account JSON key."
        CkJsonObject::ckDispose(key)
        ProcedureReturn
    EndIf

    auth.i = CkAuthGoogle::ckCreate()
    If auth.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkAuthGoogle::setCkJsonKey(auth, CkJsonObject::ckEmit(key))
    CkAuthGoogle::setCkSubEmailAddress(auth, mailbox)
    CkAuthGoogle::setCkScope(auth, "https://www.googleapis.com/auth/gmail.imap_admin")
    CkAuthGoogle::setCkExpireNumSeconds(auth, 3600)
    connection.i = CkSocket::ckCreate()
    If connection.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkSocket::setCkRequireSslCertVerify(connection, 1)
    success = CkSocket::ckConnect(connection,"www.googleapis.com",443,1,10000)
    If success <> 1
        Debug "Could not establish the Google token connection."
        CkJsonObject::ckDispose(key)
        CkAuthGoogle::ckDispose(auth)
        CkSocket::ckDispose(connection)
        ProcedureReturn
    EndIf

    success = CkAuthGoogle::ckObtainAccessToken(auth,connection)
    CkSocket::ckClose(connection,1000)
    If success <> 1
        Debug "Check the key, domain-wide delegation client ID/scope, user, and computer clock."
        CkJsonObject::ckDispose(key)
        CkAuthGoogle::ckDispose(auth)
        CkSocket::ckDispose(connection)
        ProcedureReturn
    EndIf

    imap.i = CkImap::ckCreate()
    If imap.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkImap::setCkSsl(imap, 1)
    CkImap::setCkPort(imap, 993)
    CkImap::setCkRequireSslCertVerify(imap, 1)
    CkImap::setCkAuthMethod(imap, "XOAUTH2")
    success = CkImap::ckConnect(imap,"imap.gmail.com")
    If success <> 1
        Debug "Could not establish the Gmail IMAP TLS connection."
        CkJsonObject::ckDispose(key)
        CkAuthGoogle::ckDispose(auth)
        CkSocket::ckDispose(connection)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    success = CkImap::ckLogin(imap,mailbox,CkAuthGoogle::ckAccessToken(auth))
    If success <> 1
        CkImap::ckDisconnect(imap)
        Debug "IMAP authentication failed. Check the mailbox, scope, expiry, and account policy."
        CkJsonObject::ckDispose(key)
        CkAuthGoogle::ckDispose(auth)
        CkSocket::ckDispose(connection)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    folders.i = CkMailboxes::ckCreate()
    If folders.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkImap::ckMbxList(imap,0,"","*",folders)
    If success <> 1
        CkImap::ckLogout(imap)
        CkImap::ckDisconnect(imap)
        Debug "Could not list Gmail folders."
        CkJsonObject::ckDispose(key)
        CkAuthGoogle::ckDispose(auth)
        CkSocket::ckDispose(connection)
        CkImap::ckDispose(imap)
        CkMailboxes::ckDispose(folders)
        ProcedureReturn
    EndIf

    ;  With gmail.imap_admin, labels hidden by the user's Show in IMAP setting are included.
    i.i = 0
    While (i < CkMailboxes::ckCount(folders))
        Debug CkMailboxes::ckGetName(folders,i)
        i = i + 1
    Wend
    success = CkImap::ckExamineMailbox(imap,"INBOX")
    If success <> 1
        CkImap::ckLogout(imap)
        CkImap::ckDisconnect(imap)
        Debug "Could not examine the impersonated user's INBOX."
        CkJsonObject::ckDispose(key)
        CkAuthGoogle::ckDispose(auth)
        CkSocket::ckDispose(connection)
        CkImap::ckDispose(imap)
        CkMailboxes::ckDispose(folders)
        ProcedureReturn
    EndIf

    Debug "INBOX messages: " + Str(CkImap::ckNumMessages(imap))
    CkImap::ckLogout(imap)
    CkImap::ckDisconnect(imap)


    CkJsonObject::ckDispose(key)
    CkAuthGoogle::ckDispose(auth)
    CkSocket::ckDispose(connection)
    CkImap::ckDispose(imap)
    CkMailboxes::ckDispose(folders)


    ProcedureReturn
EndProcedure