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

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

'  Assumes Chilkat is unlocked. See Global Unlock Sample.

set key = Server.CreateObject("Chilkat.JsonObject")
success = key.LoadFile("gmail-service-account.json")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not load the service-account key from the private directory.") & "</pre>"
    Response.End
End If

set auth = Server.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 = Server.CreateObject("Chilkat.Socket")
connection.RequireSslCertVerify = 1
success = connection.Connect("www.googleapis.com",443,1,10000)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not establish the Google token connection.") & "</pre>"
    Response.End
End If

success = auth.ObtainAccessToken(connection)
success = connection.Close(1000)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Token exchange failed. Check the key, delegated scopes, user, and computer clock.") & "</pre>"
    Response.End
End If

set http = Server.CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
http.AuthToken = auth.AccessToken
set resp = Server.CreateObject("Chilkat.HttpResponse")
set reply = Server.CreateObject("Chilkat.JsonObject")
success = http.HttpNoBody("GET","https://gmail.googleapis.com/gmail/v1/users/me/profile",resp)
If (resp.StatusCode = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Gmail did not return an HTTP response.") & "</pre>"
    Response.End
End If

If ((success <> 1) Or (resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Gmail HTTP " & resp.StatusCode) & "</pre>"
    success = reply.Load(resp.BodyStr)
    If (success = 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( reply.StringOf("error.message")) & "</pre>"
    End If

    Response.End
End If

success = reply.Load(resp.BodyStr)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Expected a Gmail JSON response.") & "</pre>"
    Response.End
End If

If ((reply.JsonTypeOf("emailAddress") <> 1) Or (reply.StringOfEquals("emailAddress","",1) = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Expected emailAddress in the response.") & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "Authorized mailbox: " & reply.StringOf("emailAddress")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "Messages: " & reply.IntOf("messagesTotal")) & "</pre>"

%>
</body>
</html>