Gmail IMAP: Workspace Domain-Wide Delegation
See more GMail SMTP/IMAP/POP Examples
Use a JSON service-account key to impersonate a Workspace user for IMAP with https://www.googleapis.com/auth/gmail.imap_admin. A Workspace administrator must authorize the service account's numeric OAuth client ID for this exact scope in domain-wide delegation. The impersonated user must have an active Gmail mailbox.
This IMAP-only scope reveals labels hidden by Show in IMAP and messages excluded by the user's folder-size limit. It does not authorize SMTP or POP and is not available for personal Gmail accounts or ordinary user-consent flows. Workspace administrative access policies still apply.
Protect the private key and limit who can execute the delegated application. The signed JWT exchange produces a short-lived token without a refresh token; obtain another when necessary and reconnect. Keep the computer clock accurate. This example lists folders and examines INBOX read-only without printing credentials.
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://www.googleapis.com/auth/gmail.imap_admin");
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;
}
// With gmail.imap_admin, labels hidden by the user's Show in IMAP setting are included.
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);
}