Gmail IMAP: Workspace Service Account with a JSON Key
Use a service-account JSON key with Google Workspace domain-wide delegation. A Workspace administrator must authorize the service account's numeric OAuth client ID for https://mail.google.com/. Set SubEmailAddress to the Workspace user whose active Gmail mailbox is being accessed, and use that same address for IMAP login.
A service account has no Gmail mailbox of its own; leaving the impersonated user empty does not grant access to a personal Gmail account. The signed JWT exchange yields a short-lived token without a refresh token. Obtain another when needed, protect the private key, and keep the computer clock accurate.
This example lists visible folders and examines INBOX read-only. For the different visibility rules of the IMAP-only administrative scope, see gmail.imap_admin delegation.
See the Gmail SMTP, IMAP, and POP3 guide for current authentication choices and Gmail-specific examples.
Chilkat CkPerl Downloads
use chilkat();
$success = 0;
# Assumes Chilkat is unlocked. See Global Unlock Sample.
$mailbox = 'user@example.com';
$key = chilkat::CkJsonObject->new();
$success = $key->LoadFile("gmail-service-account.json");
if ($success != 1) {
print "Could not load the private service-account key." . "\r\n";
exit;
}
if (($key->StringOfEquals("type","service_account",1) != 1) or ($key->JsonTypeOf("private_key") != 1)) {
print "Expected a Google service-account JSON key." . "\r\n";
exit;
}
$auth = chilkat::CkAuthGoogle->new();
$auth->put_JsonKey($key->emit());
$auth->put_SubEmailAddress($mailbox);
$auth->put_Scope("https://mail.google.com/");
$auth->put_ExpireNumSeconds(3600);
$connection = chilkat::CkSocket->new();
$connection->put_RequireSslCertVerify(1);
$success = $connection->Connect("www.googleapis.com",443,1,10000);
if ($success != 1) {
print "Could not establish the Google token connection." . "\r\n";
exit;
}
$success = $auth->ObtainAccessToken($connection);
$connection->Close(1000);
if ($success != 1) {
print "Check the key, domain-wide delegation client ID/scope, user, and computer clock." . "\r\n";
exit;
}
$imap = chilkat::CkImap->new();
$imap->put_Ssl(1);
$imap->put_Port(993);
$imap->put_RequireSslCertVerify(1);
$imap->put_AuthMethod("XOAUTH2");
$success = $imap->Connect("imap.gmail.com");
if ($success != 1) {
print "Could not establish the Gmail IMAP TLS connection." . "\r\n";
exit;
}
$success = $imap->Login($mailbox,$auth->accessToken());
if ($success != 1) {
$imap->Disconnect();
print "IMAP authentication failed. Check the mailbox, scope, expiry, and account policy." . "\r\n";
exit;
}
$folders = chilkat::CkMailboxes->new();
$success = $imap->MbxList(0,"","*",$folders);
if ($success != 1) {
$imap->Logout();
$imap->Disconnect();
print "Could not list Gmail folders." . "\r\n";
exit;
}
# List the labels visible through ordinary full-mail IMAP access.
$i = 0;
while (($i < $folders->get_Count())) {
print $folders->getName($i) . "\r\n";
$i = $i + 1;
}
$success = $imap->ExamineMailbox("INBOX");
if ($success != 1) {
$imap->Logout();
$imap->Disconnect();
print "Could not examine the impersonated user's INBOX." . "\r\n";
exit;
}
print "INBOX messages: " . $imap->get_NumMessages() . "\r\n";
$imap->Logout();
$imap->Disconnect();