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.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoKey
Handle hoAuth
Variant vConnection
Handle hoConnection
Handle hoHttp
Variant vResp
Handle hoResp
Handle hoReply
String sTemp1
Integer iTemp1
Integer iTemp2
Boolean bTemp1
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
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 service-account key from the private directory."
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 ComScope Of hoAuth To "https://www.googleapis.com/auth/gmail.metadata"
Set ComSubEmailAddress Of hoAuth To "user@example.com"
Set ComExpireNumSeconds Of hoAuth To 3600
// Obtain a token for the named Workspace user. Keep the computer clock accurate.
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 "Token exchange failed. Check the key, delegated scopes, user, and computer clock."
Procedure_Return
End
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Set ComRequireSslCertVerify Of hoHttp To True
Get ComAccessToken Of hoAuth To sTemp1
Set ComAuthToken Of hoHttp To sTemp1
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get Create (RefClass(cComChilkatJsonObject)) To hoReply
If (Not(IsComObjectCreated(hoReply))) Begin
Send CreateComObject of hoReply
End
Get pvComObject of hoResp to vResp
Get ComHttpNoBody Of hoHttp "GET" "https://gmail.googleapis.com/gmail/v1/users/me/profile" vResp To iSuccess
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 0) Begin
Showln "Gmail did not return an HTTP response."
Procedure_Return
End
Get ComStatusCode Of hoResp To iTemp1
Get ComStatusCode Of hoResp To iTemp2
If ((iSuccess <> True) Or (iTemp1 < 200) Or (iTemp2 >= 300)) Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Gmail HTTP " iTemp1
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoReply sTemp1 To iSuccess
If (iSuccess = True) Begin
Get ComStringOf Of hoReply "error.message" To sTemp1
Showln sTemp1
End
Procedure_Return
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoReply sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Expected a Gmail JSON response."
Procedure_Return
End
Get ComJsonTypeOf Of hoReply "emailAddress" To iTemp1
Get ComStringOfEquals Of hoReply "emailAddress" "" True To bTemp1
If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
Showln "Expected emailAddress in the response."
Procedure_Return
End
Get ComStringOf Of hoReply "emailAddress" To sTemp1
Showln "Authorized mailbox: " sTemp1
Get ComIntOf Of hoReply "messagesTotal" To iTemp1
Showln "Messages: " iTemp1
End_Procedure