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 AutoIt Downloads
Local $bSuccess = False
; Assumes Chilkat is unlocked. See Global Unlock Sample.
$oKey = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oKey.LoadFile("gmail-service-account.json")
If ($bSuccess <> True) Then
ConsoleWrite("Could not load the service-account key from the private directory." & @CRLF)
Exit
EndIf
$oAuth = ObjCreate("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 = ObjCreate("Chilkat.Socket")
$oConnection.RequireSslCertVerify = True
$bSuccess = $oConnection.Connect("www.googleapis.com",443,True,10000)
If ($bSuccess <> True) Then
ConsoleWrite("Could not establish the Google token connection." & @CRLF)
Exit
EndIf
$bSuccess = $oAuth.ObtainAccessToken($oConnection)
$oConnection.Close(1000)
If ($bSuccess <> True) Then
ConsoleWrite("Token exchange failed. Check the key, delegated scopes, user, and computer clock." & @CRLF)
Exit
EndIf
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
$oHttp.AuthToken = $oAuth.AccessToken
$oResp = ObjCreate("Chilkat.HttpResponse")
$oReply = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oHttp.HttpNoBody("GET","https://gmail.googleapis.com/gmail/v1/users/me/profile",$oResp)
If ($oResp.StatusCode = 0) Then
ConsoleWrite("Gmail did not return an HTTP response." & @CRLF)
Exit
EndIf
If (($bSuccess <> True) Or ($oResp.StatusCode < 200) Or ($oResp.StatusCode >= 300)) Then
ConsoleWrite("Gmail HTTP " & $oResp.StatusCode & @CRLF)
$bSuccess = $oReply.Load($oResp.BodyStr)
If ($bSuccess = True) Then
ConsoleWrite($oReply.StringOf("error.message") & @CRLF)
EndIf
Exit
EndIf
$bSuccess = $oReply.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Expected a Gmail JSON response." & @CRLF)
Exit
EndIf
If (($oReply.JsonTypeOf("emailAddress") <> 1) Or ($oReply.StringOfEquals("emailAddress","",True) = True)) Then
ConsoleWrite("Expected emailAddress in the response." & @CRLF)
Exit
EndIf
ConsoleWrite("Authorized mailbox: " & $oReply.StringOf("emailAddress") & @CRLF)
ConsoleWrite("Messages: " & $oReply.IntOf("messagesTotal") & @CRLF)