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 Delphi DLL Downloads
var
success: Boolean;
key: HCkJsonObject;
auth: HCkAuthGoogle;
connection: HCkSocket;
http: HCkHttp;
resp: HCkHttpResponse;
reply: HCkJsonObject;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
key := CkJsonObject_Create();
success := CkJsonObject_LoadFile(key,'gmail-service-account.json');
if (success <> True) then
begin
Memo1.Lines.Add('Could not load the service-account key from the private directory.');
Exit;
end;
auth := CkAuthGoogle_Create();
CkAuthGoogle_putJsonKey(auth,CkJsonObject__emit(key));
CkAuthGoogle_putScope(auth,'https://www.googleapis.com/auth/gmail.metadata');
CkAuthGoogle_putSubEmailAddress(auth,'user@example.com');
CkAuthGoogle_putExpireNumSeconds(auth,3600);
// Obtain a token for the named Workspace user. Keep the computer clock accurate.
connection := CkSocket_Create();
CkSocket_putRequireSslCertVerify(connection,True);
success := CkSocket_Connect(connection,'www.googleapis.com',443,True,10000);
if (success <> True) then
begin
Memo1.Lines.Add('Could not establish the Google token connection.');
Exit;
end;
success := CkAuthGoogle_ObtainAccessToken(auth,connection);
CkSocket_Close(connection,1000);
if (success <> True) then
begin
Memo1.Lines.Add('Token exchange failed. Check the key, delegated scopes, user, and computer clock.');
Exit;
end;
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putAuthToken(http,CkAuthGoogle__accessToken(auth));
resp := CkHttpResponse_Create();
reply := CkJsonObject_Create();
success := CkHttp_HttpNoBody(http,'GET','https://gmail.googleapis.com/gmail/v1/users/me/profile',resp);
if (CkHttpResponse_getStatusCode(resp) = 0) then
begin
Memo1.Lines.Add('Gmail did not return an HTTP response.');
Exit;
end;
if ((success <> True) or (CkHttpResponse_getStatusCode(resp) < 200) or (CkHttpResponse_getStatusCode(resp) >= 300)) then
begin
Memo1.Lines.Add('Gmail HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
success := CkJsonObject_Load(reply,CkHttpResponse__bodyStr(resp));
if (success = True) then
begin
Memo1.Lines.Add(CkJsonObject__stringOf(reply,'error.message'));
end;
Exit;
end;
success := CkJsonObject_Load(reply,CkHttpResponse__bodyStr(resp));
if (success <> True) then
begin
Memo1.Lines.Add('Expected a Gmail JSON response.');
Exit;
end;
if ((CkJsonObject_JsonTypeOf(reply,'emailAddress') <> 1) or (CkJsonObject_StringOfEquals(reply,'emailAddress','',True) = True)) then
begin
Memo1.Lines.Add('Expected emailAddress in the response.');
Exit;
end;
Memo1.Lines.Add('Authorized mailbox: ' + CkJsonObject__stringOf(reply,'emailAddress'));
Memo1.Lines.Add('Messages: ' + IntToStr(CkJsonObject_IntOf(reply,'messagesTotal')));
CkJsonObject_Dispose(key);
CkAuthGoogle_Dispose(auth);
CkSocket_Dispose(connection);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(reply);