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

VBScript
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)

success = 0

'  Assumes Chilkat is unlocked. See Global Unlock Sample.

set key = CreateObject("Chilkat.JsonObject")
success = key.LoadFile("gmail-service-account.json")
If (success <> 1) Then
    outFile.WriteLine("Could not load the service-account key from the private directory.")
    WScript.Quit
End If

set auth = CreateObject("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.
set connection = CreateObject("Chilkat.Socket")
connection.RequireSslCertVerify = 1
success = connection.Connect("www.googleapis.com",443,1,10000)
If (success <> 1) Then
    outFile.WriteLine("Could not establish the Google token connection.")
    WScript.Quit
End If

success = auth.ObtainAccessToken(connection)
success = connection.Close(1000)
If (success <> 1) Then
    outFile.WriteLine("Token exchange failed. Check the key, delegated scopes, user, and computer clock.")
    WScript.Quit
End If

set http = CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
http.AuthToken = auth.AccessToken
set resp = CreateObject("Chilkat.HttpResponse")
set reply = CreateObject("Chilkat.JsonObject")
success = http.HttpNoBody("GET","https://gmail.googleapis.com/gmail/v1/users/me/profile",resp)
If (resp.StatusCode = 0) Then
    outFile.WriteLine("Gmail did not return an HTTP response.")
    WScript.Quit
End If

If ((success <> 1) Or (resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
    outFile.WriteLine("Gmail HTTP " & resp.StatusCode)
    success = reply.Load(resp.BodyStr)
    If (success = 1) Then
        outFile.WriteLine(reply.StringOf("error.message"))
    End If

    WScript.Quit
End If

success = reply.Load(resp.BodyStr)
If (success <> 1) Then
    outFile.WriteLine("Expected a Gmail JSON response.")
    WScript.Quit
End If

If ((reply.JsonTypeOf("emailAddress") <> 1) Or (reply.StringOfEquals("emailAddress","",1) = 1)) Then
    outFile.WriteLine("Expected emailAddress in the response.")
    WScript.Quit
End If

outFile.WriteLine("Authorized mailbox: " & reply.StringOf("emailAddress"))
outFile.WriteLine("Messages: " & reply.IntOf("messagesTotal"))

outFile.Close