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 C Downloads
#include <C_CkJsonObject.h>
#include <C_CkAuthGoogle.h>
#include <C_CkSocket.h>
#include <C_CkImap.h>
#include <C_CkMailboxes.h>
void ChilkatSample(void)
{
BOOL success;
const char *mailbox;
HCkJsonObject key;
HCkAuthGoogle auth;
HCkSocket connection;
HCkImap imap;
HCkMailboxes folders;
int i;
success = FALSE;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
mailbox = "user@example.com";
key = CkJsonObject_Create();
success = CkJsonObject_LoadFile(key,"gmail-service-account.json");
if (success != TRUE) {
printf("Could not load the private service-account key.\n");
CkJsonObject_Dispose(key);
return;
}
if ((CkJsonObject_StringOfEquals(key,"type","service_account",TRUE) != TRUE) || (CkJsonObject_JsonTypeOf(key,"private_key") != 1)) {
printf("Expected a Google service-account JSON key.\n");
CkJsonObject_Dispose(key);
return;
}
auth = CkAuthGoogle_Create();
CkAuthGoogle_putJsonKey(auth,CkJsonObject_emit(key));
CkAuthGoogle_putSubEmailAddress(auth,mailbox);
CkAuthGoogle_putScope(auth,"https://mail.google.com/");
CkAuthGoogle_putExpireNumSeconds(auth,3600);
connection = CkSocket_Create();
CkSocket_putRequireSslCertVerify(connection,TRUE);
success = CkSocket_Connect(connection,"www.googleapis.com",443,TRUE,10000);
if (success != TRUE) {
printf("Could not establish the Google token connection.\n");
CkJsonObject_Dispose(key);
CkAuthGoogle_Dispose(auth);
CkSocket_Dispose(connection);
return;
}
success = CkAuthGoogle_ObtainAccessToken(auth,connection);
CkSocket_Close(connection,1000);
if (success != TRUE) {
printf("Check the key, domain-wide delegation client ID/scope, user, and computer clock.\n");
CkJsonObject_Dispose(key);
CkAuthGoogle_Dispose(auth);
CkSocket_Dispose(connection);
return;
}
imap = CkImap_Create();
CkImap_putSsl(imap,TRUE);
CkImap_putPort(imap,993);
CkImap_putRequireSslCertVerify(imap,TRUE);
CkImap_putAuthMethod(imap,"XOAUTH2");
success = CkImap_Connect(imap,"imap.gmail.com");
if (success != TRUE) {
printf("Could not establish the Gmail IMAP TLS connection.\n");
CkJsonObject_Dispose(key);
CkAuthGoogle_Dispose(auth);
CkSocket_Dispose(connection);
CkImap_Dispose(imap);
return;
}
success = CkImap_Login(imap,mailbox,CkAuthGoogle_accessToken(auth));
if (success != TRUE) {
CkImap_Disconnect(imap);
printf("IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.\n");
CkJsonObject_Dispose(key);
CkAuthGoogle_Dispose(auth);
CkSocket_Dispose(connection);
CkImap_Dispose(imap);
return;
}
folders = CkMailboxes_Create();
success = CkImap_MbxList(imap,FALSE,"","*",folders);
if (success != TRUE) {
CkImap_Logout(imap);
CkImap_Disconnect(imap);
printf("Could not list Gmail folders.\n");
CkJsonObject_Dispose(key);
CkAuthGoogle_Dispose(auth);
CkSocket_Dispose(connection);
CkImap_Dispose(imap);
CkMailboxes_Dispose(folders);
return;
}
// List the labels visible through ordinary full-mail IMAP access.
i = 0;
while ((i < CkMailboxes_getCount(folders))) {
printf("%s\n",CkMailboxes_getName(folders,i));
i = i + 1;
}
success = CkImap_ExamineMailbox(imap,"INBOX");
if (success != TRUE) {
CkImap_Logout(imap);
CkImap_Disconnect(imap);
printf("Could not examine the impersonated user's INBOX.\n");
CkJsonObject_Dispose(key);
CkAuthGoogle_Dispose(auth);
CkSocket_Dispose(connection);
CkImap_Dispose(imap);
CkMailboxes_Dispose(folders);
return;
}
printf("INBOX messages: %d\n",CkImap_getNumMessages(imap));
CkImap_Logout(imap);
CkImap_Disconnect(imap);
CkJsonObject_Dispose(key);
CkAuthGoogle_Dispose(auth);
CkSocket_Dispose(connection);
CkImap_Dispose(imap);
CkMailboxes_Dispose(folders);
}