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

Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat v11.4.0 or greater.
JavaScript
var success = false;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

var key = new CkJsonObject();
success = key.LoadFile("gmail-service-account.json");
if (success !== true) {
    console.log("Could not load the service-account key from the private directory.");
    return;
}

var auth = new CkAuthGoogle();
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.
var connection = new CkSocket();
connection.RequireSslCertVerify = true;
success = connection.Connect("www.googleapis.com",443,true,10000);
if (success !== true) {
    console.log("Could not establish the Google token connection.");
    return;
}

success = auth.ObtainAccessToken(connection);
connection.Close(1000);
if (success !== true) {
    console.log("Token exchange failed. Check the key, delegated scopes, user, and computer clock.");
    return;
}

var http = new CkHttp();
http.RequireSslCertVerify = true;
http.AuthToken = auth.AccessToken;
var resp = new CkHttpResponse();
var reply = new CkJsonObject();
success = http.HttpNoBody("GET","https://gmail.googleapis.com/gmail/v1/users/me/profile",resp);
if (resp.StatusCode == 0) {
    console.log("Gmail did not return an HTTP response.");
    return;
}

if ((success !== true) || (resp.StatusCode < 200) || (resp.StatusCode >= 300)) {
    console.log("Gmail HTTP " + resp.StatusCode);
    success = reply.Load(resp.BodyStr);
    if (success == true) {
        console.log(reply.StringOf("error.message"));
    }

    return;
}

success = reply.Load(resp.BodyStr);
if (success !== true) {
    console.log("Expected a Gmail JSON response.");
    return;
}

if ((reply.JsonTypeOf("emailAddress") !== 1) || (reply.StringOfEquals("emailAddress","",true) == true)) {
    console.log("Expected emailAddress in the response.");
    return;
}

console.log("Authorized mailbox: " + reply.StringOf("emailAddress"));
console.log("Messages: " + reply.IntOf("messagesTotal"));