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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loKey
LOCAL loAuth
LOCAL loConnection
LOCAL loHttp
LOCAL loResp
LOCAL loReply
lnSuccess = 0
* Assumes Chilkat is unlocked. See Global Unlock Sample.
loKey = CreateObject('Chilkat.JsonObject')
lnSuccess = loKey.LoadFile("gmail-service-account.json")
IF (lnSuccess <> 1) THEN
? "Could not load the service-account key from the private directory."
RELEASE loKey
CANCEL
ENDIF
loAuth = CreateObject('Chilkat.AuthGoogle')
loAuth.JsonKey = loKey.Emit()
loAuth.Scope = "https://www.googleapis.com/auth/gmail.metadata"
loAuth.SubEmailAddress = "user@example.com"
loAuth.ExpireNumSeconds = 3600
* Obtain a token for the named Workspace user. Keep the computer clock accurate.
loConnection = CreateObject('Chilkat.Socket')
loConnection.RequireSslCertVerify = 1
lnSuccess = loConnection.Connect("www.googleapis.com",443,1,10000)
IF (lnSuccess <> 1) THEN
? "Could not establish the Google token connection."
RELEASE loKey
RELEASE loAuth
RELEASE loConnection
CANCEL
ENDIF
lnSuccess = loAuth.ObtainAccessToken(loConnection)
loConnection.Close(1000)
IF (lnSuccess <> 1) THEN
? "Token exchange failed. Check the key, delegated scopes, user, and computer clock."
RELEASE loKey
RELEASE loAuth
RELEASE loConnection
CANCEL
ENDIF
loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
loHttp.AuthToken = loAuth.AccessToken
loResp = CreateObject('Chilkat.HttpResponse')
loReply = CreateObject('Chilkat.JsonObject')
lnSuccess = loHttp.HttpNoBody("GET","https://gmail.googleapis.com/gmail/v1/users/me/profile",loResp)
IF (loResp.StatusCode = 0) THEN
? "Gmail did not return an HTTP response."
RELEASE loKey
RELEASE loAuth
RELEASE loConnection
RELEASE loHttp
RELEASE loResp
RELEASE loReply
CANCEL
ENDIF
IF ((lnSuccess <> 1) OR (loResp.StatusCode < 200) OR (loResp.StatusCode >= 300)) THEN
? "Gmail HTTP " + STR(loResp.StatusCode)
lnSuccess = loReply.Load(loResp.BodyStr)
IF (lnSuccess = 1) THEN
? loReply.StringOf("error.message")
ENDIF
RELEASE loKey
RELEASE loAuth
RELEASE loConnection
RELEASE loHttp
RELEASE loResp
RELEASE loReply
CANCEL
ENDIF
lnSuccess = loReply.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
? "Expected a Gmail JSON response."
RELEASE loKey
RELEASE loAuth
RELEASE loConnection
RELEASE loHttp
RELEASE loResp
RELEASE loReply
CANCEL
ENDIF
IF ((loReply.JsonTypeOf("emailAddress") <> 1) OR (loReply.StringOfEquals("emailAddress","",1) = 1)) THEN
? "Expected emailAddress in the response."
RELEASE loKey
RELEASE loAuth
RELEASE loConnection
RELEASE loHttp
RELEASE loResp
RELEASE loReply
CANCEL
ENDIF
? "Authorized mailbox: " + loReply.StringOf("emailAddress")
? "Messages: " + STR(loReply.IntOf("messagesTotal"))
RELEASE loKey
RELEASE loAuth
RELEASE loConnection
RELEASE loHttp
RELEASE loResp
RELEASE loReply