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 CkJava Downloads
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
boolean success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
String mailbox = "user@example.com";
CkJsonObject key = new CkJsonObject();
success = key.LoadFile("gmail-service-account.json");
if (success != true) {
System.out.println("Could not load the private service-account key.");
return;
}
if ((key.StringOfEquals("type","service_account",true) != true) || (key.JsonTypeOf("private_key") != 1)) {
System.out.println("Expected a Google service-account JSON key.");
return;
}
CkAuthGoogle auth = new CkAuthGoogle();
auth.put_JsonKey(key.emit());
auth.put_SubEmailAddress(mailbox);
auth.put_Scope("https://mail.google.com/");
auth.put_ExpireNumSeconds(3600);
CkSocket connection = new CkSocket();
connection.put_RequireSslCertVerify(true);
success = connection.Connect("www.googleapis.com",443,true,10000);
if (success != true) {
System.out.println("Could not establish the Google token connection.");
return;
}
success = auth.ObtainAccessToken(connection);
connection.Close(1000);
if (success != true) {
System.out.println("Check the key, domain-wide delegation client ID/scope, user, and computer clock.");
return;
}
CkImap imap = new CkImap();
imap.put_Ssl(true);
imap.put_Port(993);
imap.put_RequireSslCertVerify(true);
imap.put_AuthMethod("XOAUTH2");
success = imap.Connect("imap.gmail.com");
if (success != true) {
System.out.println("Could not establish the Gmail IMAP TLS connection.");
return;
}
success = imap.Login(mailbox,auth.accessToken());
if (success != true) {
imap.Disconnect();
System.out.println("IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.");
return;
}
CkMailboxes folders = new CkMailboxes();
success = imap.MbxList(false,"","*",folders);
if (success != true) {
imap.Logout();
imap.Disconnect();
System.out.println("Could not list Gmail folders.");
return;
}
// List the labels visible through ordinary full-mail IMAP access.
int i = 0;
while ((i < folders.get_Count())) {
System.out.println(folders.getName(i));
i = i+1;
}
success = imap.ExamineMailbox("INBOX");
if (success != true) {
imap.Logout();
imap.Disconnect();
System.out.println("Could not examine the impersonated user's INBOX.");
return;
}
System.out.println("INBOX messages: " + imap.get_NumMessages());
imap.Logout();
imap.Disconnect();
}
}