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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    String sMailbox
    Handle hoKey
    Handle hoAuth
    Variant vConnection
    Handle hoConnection
    Handle hoImap
    Variant vFolders
    Handle hoFolders
    Integer i
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

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

    Move "user@example.com" To sMailbox
    Get Create (RefClass(cComChilkatJsonObject)) To hoKey
    If (Not(IsComObjectCreated(hoKey))) Begin
        Send CreateComObject of hoKey
    End
    Get ComLoadFile Of hoKey "gmail-service-account.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not load the private service-account key."
        Procedure_Return
    End

    Get ComStringOfEquals Of hoKey "type" "service_account" True To bTemp1
    Get ComJsonTypeOf Of hoKey "private_key" To iTemp1
    If ((bTemp1 <> True) Or (iTemp1 <> 1)) Begin
        Showln "Expected a Google service-account JSON key."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatAuthGoogle)) To hoAuth
    If (Not(IsComObjectCreated(hoAuth))) Begin
        Send CreateComObject of hoAuth
    End
    Get ComEmit Of hoKey To sTemp1
    Set ComJsonKey Of hoAuth To sTemp1
    Set ComSubEmailAddress Of hoAuth To sMailbox
    Set ComScope Of hoAuth To "https://www.googleapis.com/auth/gmail.imap_admin"
    Set ComExpireNumSeconds Of hoAuth To 3600
    Get Create (RefClass(cComChilkatSocket)) To hoConnection
    If (Not(IsComObjectCreated(hoConnection))) Begin
        Send CreateComObject of hoConnection
    End
    Set ComRequireSslCertVerify Of hoConnection To True
    Get ComConnect Of hoConnection "www.googleapis.com" 443 True 10000 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not establish the Google token connection."
        Procedure_Return
    End

    Get pvComObject of hoConnection to vConnection
    Get ComObtainAccessToken Of hoAuth vConnection To iSuccess
    Get ComClose Of hoConnection 1000 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Check the key, domain-wide delegation client ID/scope, user, and computer clock."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatImap)) To hoImap
    If (Not(IsComObjectCreated(hoImap))) Begin
        Send CreateComObject of hoImap
    End
    Set ComSsl Of hoImap To True
    Set ComPort Of hoImap To 993
    Set ComRequireSslCertVerify Of hoImap To True
    Set ComAuthMethod Of hoImap To "XOAUTH2"
    Get ComConnect Of hoImap "imap.gmail.com" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not establish the Gmail IMAP TLS connection."
        Procedure_Return
    End

    Get ComAccessToken Of hoAuth To sTemp1
    Get ComLogin Of hoImap sMailbox sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Get ComDisconnect Of hoImap To iSuccess
        Showln "IMAP authentication failed. Check the mailbox, scope, expiry, and account policy."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatMailboxes)) To hoFolders
    If (Not(IsComObjectCreated(hoFolders))) Begin
        Send CreateComObject of hoFolders
    End
    Get pvComObject of hoFolders to vFolders
    Get ComMbxList Of hoImap False "" "*" vFolders To iSuccess
    If (iSuccess <> True) Begin
        Get ComLogout Of hoImap To iSuccess
        Get ComDisconnect Of hoImap To iSuccess
        Showln "Could not list Gmail folders."
        Procedure_Return
    End

    //  With gmail.imap_admin, labels hidden by the user's Show in IMAP setting are included.
    Move 0 To i
    While (i < (ComCount(hoFolders)))
        Get ComGetName Of hoFolders i To sTemp1
        Showln sTemp1
        Move (i + 1) To i
    Loop

    Get ComExamineMailbox Of hoImap "INBOX" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLogout Of hoImap To iSuccess
        Get ComDisconnect Of hoImap To iSuccess
        Showln "Could not examine the impersonated user's INBOX."
        Procedure_Return
    End

    Get ComNumMessages Of hoImap To iTemp1
    Showln "INBOX messages: " iTemp1
    Get ComLogout Of hoImap To iSuccess
    Get ComDisconnect Of hoImap To iSuccess


End_Procedure