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 VB.NET Downloads
Dim success As Boolean = False
' Assumes Chilkat is unlocked. See Global Unlock Sample.
Dim key As New Chilkat.JsonObject
success = key.LoadFile("gmail-service-account.json")
If (success <> True) Then
Debug.WriteLine("Could not load the service-account key from the private directory.")
Exit Sub
End If
Dim auth As New Chilkat.AuthGoogle
auth.JsonKey = key.Emit()
auth.Scope = "https://www.googleapis.com/auth/gmail.metadata"
auth.SubEmailAddress = "user@example.com"
auth.ExpireNumSeconds = 3600
' Obtain a token for the named Workspace user. Keep the computer clock accurate.
Dim connection As New Chilkat.Socket
connection.RequireSslCertVerify = True
success = connection.Connect("www.googleapis.com",443,True,10000)
If (success <> True) Then
Debug.WriteLine("Could not establish the Google token connection.")
Exit Sub
End If
success = auth.ObtainAccessToken(connection)
connection.Close(1000)
If (success <> True) Then
Debug.WriteLine("Token exchange failed. Check the key, delegated scopes, user, and computer clock.")
Exit Sub
End If
Dim http As New Chilkat.Http
http.RequireSslCertVerify = True
http.AuthToken = auth.AccessToken
Dim resp As New Chilkat.HttpResponse
Dim reply As New Chilkat.JsonObject
success = http.HttpNoBody("GET","https://gmail.googleapis.com/gmail/v1/users/me/profile",resp)
If (resp.StatusCode = 0) Then
Debug.WriteLine("Gmail did not return an HTTP response.")
Exit Sub
End If
If ((success <> True) Or (resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
Debug.WriteLine("Gmail HTTP " & resp.StatusCode)
success = reply.Load(resp.BodyStr)
If (success = True) Then
Debug.WriteLine(reply.StringOf("error.message"))
End If
Exit Sub
End If
success = reply.Load(resp.BodyStr)
If (success <> True) Then
Debug.WriteLine("Expected a Gmail JSON response.")
Exit Sub
End If
If ((reply.JsonTypeOf("emailAddress") <> 1) Or (reply.StringOfEquals("emailAddress","",True) = True)) Then
Debug.WriteLine("Expected emailAddress in the response.")
Exit Sub
End If
Debug.WriteLine("Authorized mailbox: " & reply.StringOf("emailAddress"))
Debug.WriteLine("Messages: " & reply.IntOf("messagesTotal"))