Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.0.0+

Access a Google Workspace mailbox with a service account

See more GMail REST API Examples

A Google Workspace super administrator must authorize the service account client ID for https://www.googleapis.com/auth/gmail.metadata in Admin console domain-wide delegation. Enable the Gmail API in the Cloud project. Set SubEmailAddress to a Gmail-enabled user in that organization. The profile response identifies the mailbox represented by me.

Store the downloaded JSON service-account key in gmail-service-account.json in a private working directory. AuthGoogle signs a JWT with the key and exchanges it for an access token. Repeat that exchange when the token expires. For other operations, change Scope and grant that scope in the Admin console. This workflow is for organization-controlled applications. Personal Gmail accounts can use offline authorization with a refresh token.

Google documentation

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Key
oleobject loo_Auth
oleobject loo_Connection
oleobject loo_Http
oleobject loo_Resp
oleobject loo_Reply

li_Success = 0

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

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 service-account key from the private directory."
    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.Scope = "https://www.googleapis.com/auth/gmail.metadata"
loo_Auth.SubEmailAddress = "user@example.com"
loo_Auth.ExpireNumSeconds = 3600

//  Obtain a token for the named Workspace user. Keep the computer clock accurate.
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 "Token exchange failed. Check the key, delegated scopes, user, and computer clock."
    destroy loo_Key
    destroy loo_Auth
    destroy loo_Connection
    return
end if

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")

loo_Http.RequireSslCertVerify = 1
loo_Http.AuthToken = loo_Auth.AccessToken
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

loo_Reply = create oleobject
li_rc = loo_Reply.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Http.HttpNoBody("GET","https://gmail.googleapis.com/gmail/v1/users/me/profile",loo_Resp)
if loo_Resp.StatusCode = 0 then
    Write-Debug "Gmail did not return an HTTP response."
    destroy loo_Key
    destroy loo_Auth
    destroy loo_Connection
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Reply
    return
end if

if (li_Success <> 1) OR (loo_Resp.StatusCode < 200) OR (loo_Resp.StatusCode >= 300) then
    Write-Debug "Gmail HTTP " + string(loo_Resp.StatusCode)
    li_Success = loo_Reply.Load(loo_Resp.BodyStr)
    if li_Success = 1 then
        Write-Debug loo_Reply.StringOf("error.message")
    end if

    destroy loo_Key
    destroy loo_Auth
    destroy loo_Connection
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Reply
    return
end if

li_Success = loo_Reply.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
    Write-Debug "Expected a Gmail JSON response."
    destroy loo_Key
    destroy loo_Auth
    destroy loo_Connection
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Reply
    return
end if

if (loo_Reply.JsonTypeOf("emailAddress") <> 1) OR (loo_Reply.StringOfEquals("emailAddress","",1) = 1) then
    Write-Debug "Expected emailAddress in the response."
    destroy loo_Key
    destroy loo_Auth
    destroy loo_Connection
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Reply
    return
end if

Write-Debug "Authorized mailbox: " + loo_Reply.StringOf("emailAddress")
Write-Debug "Messages: " + string(loo_Reply.IntOf("messagesTotal"))


destroy loo_Key
destroy loo_Auth
destroy loo_Connection
destroy loo_Http
destroy loo_Resp
destroy loo_Reply