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

Xbase++
LOCAL nSuccess
LOCAL oKey
LOCAL oAuth
LOCAL oConnection
LOCAL oHttp
LOCAL oResp
LOCAL oReply

nSuccess := 0

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

oKey := CreateObject("Chilkat.JsonObject")
nSuccess := oKey:LoadFile("gmail-service-account.json")
IF (nSuccess != 1)
    ? "Could not load the service-account key from the private directory."
    oKey:destroy()
    RETURN
ENDIF

oAuth := CreateObject("Chilkat.AuthGoogle")
oAuth:JsonKey := oKey:Emit()
oAuth:Scope := "https://www.googleapis.com/auth/gmail.metadata"
oAuth:SubEmailAddress := "user@example.com"
oAuth:ExpireNumSeconds := 3600

//  Obtain a token for the named Workspace user. Keep the computer clock accurate.
oConnection := CreateObject("Chilkat.Socket")
oConnection:RequireSslCertVerify := 1
nSuccess := oConnection:Connect("www.googleapis.com", 443, 1, 10000)
IF (nSuccess != 1)
    ? "Could not establish the Google token connection."
    oKey:destroy()
    oAuth:destroy()
    oConnection:destroy()
    RETURN
ENDIF

nSuccess := oAuth:ObtainAccessToken(oConnection)
oConnection:Close(1000)
IF (nSuccess != 1)
    ? "Token exchange failed. Check the key, delegated scopes, user, and computer clock."
    oKey:destroy()
    oAuth:destroy()
    oConnection:destroy()
    RETURN
ENDIF

oHttp := CreateObject("Chilkat.Http")
oHttp:RequireSslCertVerify := 1
oHttp:AuthToken := oAuth:AccessToken
oResp := CreateObject("Chilkat.HttpResponse")
oReply := CreateObject("Chilkat.JsonObject")
nSuccess := oHttp:HttpNoBody("GET", "https://gmail.googleapis.com/gmail/v1/users/me/profile", oResp)
IF (oResp:StatusCode == 0)
    ? "Gmail did not return an HTTP response."
    oKey:destroy()
    oAuth:destroy()
    oConnection:destroy()
    oHttp:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

IF ((nSuccess != 1) .OR. (oResp:StatusCode < 200) .OR. (oResp:StatusCode >= 300))
    ? "Gmail HTTP " + Str(oResp:StatusCode)
    nSuccess := oReply:Load(oResp:BodyStr)
    IF (nSuccess == 1)
        ? oReply:StringOf("error.message")
    ENDIF

    oKey:destroy()
    oAuth:destroy()
    oConnection:destroy()
    oHttp:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

nSuccess := oReply:Load(oResp:BodyStr)
IF (nSuccess != 1)
    ? "Expected a Gmail JSON response."
    oKey:destroy()
    oAuth:destroy()
    oConnection:destroy()
    oHttp:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

IF ((oReply:JsonTypeOf("emailAddress") != 1) .OR. (oReply:StringOfEquals("emailAddress", "", 1) == 1))
    ? "Expected emailAddress in the response."
    oKey:destroy()
    oAuth:destroy()
    oConnection:destroy()
    oHttp:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

? "Authorized mailbox: " + oReply:StringOf("emailAddress")
? "Messages: " + Str(oReply:IntOf("messagesTotal"))

oKey:destroy()
oAuth:destroy()
oConnection:destroy()
oHttp:destroy()
oResp:destroy()
oReply:destroy()