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 Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
var success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
final mailbox = 'user@example.com';
final key = CkJsonObject();
try {
key.loadFile('gmail-service-account.json');
} on ChilkatException {
print('Could not load the private service-account key.');
return;
}
if ((!key.stringOfEquals('type', 'service_account', true)) || (key.jsonTypeOf('private_key') != 1)) {
print('Expected a Google service-account JSON key.');
return;
}
final auth = CkAuthGoogle();
auth.jsonKey = key.emit();
auth.subEmailAddress = mailbox;
auth.scope = 'https://www.googleapis.com/auth/gmail.imap_admin';
auth.expireNumSeconds = 3600;
final connection = CkSocket();
connection.requireSslCertVerify = true;
try {
connection.connect('www.googleapis.com', 443, true, 10000);
} on ChilkatException {
print('Could not establish the Google token connection.');
return;
}
success = true;
try {
auth.obtainAccessToken(connection);
} on ChilkatException {
success = false;
}
connection.close(1000);
if (!success) {
print('Check the key, domain-wide delegation client ID/scope, user, and computer clock.');
return;
}
final imap = CkImap();
imap.ssl = true;
imap.port = 993;
imap.requireSslCertVerify = true;
imap.authMethod = 'XOAUTH2';
try {
imap.connect('imap.gmail.com');
} on ChilkatException {
print('Could not establish the Gmail IMAP TLS connection.');
return;
}
try {
imap.login(mailbox, auth.accessToken);
} on ChilkatException {
imap.disconnect();
print('IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.');
return;
}
final folders = CkMailboxes();
try {
imap.mbxList(false, '', '*', folders);
} on ChilkatException {
imap.logout();
imap.disconnect();
print('Could not list Gmail folders.');
return;
}
// With gmail.imap_admin, labels hidden by the user's Show in IMAP setting are included.
var i = 0;
while (i < folders.count) {
print(folders.getName(i));
i++;
}
try {
imap.examineMailbox('INBOX');
} on ChilkatException {
imap.logout();
imap.disconnect();
print('Could not examine the impersonated user\'s INBOX.');
return;
}
print('INBOX messages: ${imap.numMessages}');
imap.logout();
imap.disconnect();
}