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 Go Downloads
success := false
// Assumes Chilkat is unlocked. See Global Unlock Sample.
mailbox := "user@example.com"
key := chilkat.NewJsonObject()
success = key.LoadFile("gmail-service-account.json")
if success != true {
fmt.Println("Could not load the private service-account key.")
key.DisposeJsonObject()
return
}
if (key.StringOfEquals("type","service_account",true) != true) || (key.JsonTypeOf("private_key") != 1) {
fmt.Println("Expected a Google service-account JSON key.")
key.DisposeJsonObject()
return
}
auth := chilkat.NewAuthGoogle()
auth.SetJsonKey(key.Emit())
auth.SetSubEmailAddress(mailbox)
auth.SetScope("https://mail.google.com/")
auth.SetExpireNumSeconds(3600)
connection := chilkat.NewSocket()
connection.SetRequireSslCertVerify(true)
success = connection.Connect("www.googleapis.com",443,true,10000)
if success != true {
fmt.Println("Could not establish the Google token connection.")
key.DisposeJsonObject()
auth.DisposeAuthGoogle()
connection.DisposeSocket()
return
}
success = auth.ObtainAccessToken(connection)
connection.Close(1000)
if success != true {
fmt.Println("Check the key, domain-wide delegation client ID/scope, user, and computer clock.")
key.DisposeJsonObject()
auth.DisposeAuthGoogle()
connection.DisposeSocket()
return
}
imap := chilkat.NewImap()
imap.SetSsl(true)
imap.SetPort(993)
imap.SetRequireSslCertVerify(true)
imap.SetAuthMethod("XOAUTH2")
success = imap.Connect("imap.gmail.com")
if success != true {
fmt.Println("Could not establish the Gmail IMAP TLS connection.")
key.DisposeJsonObject()
auth.DisposeAuthGoogle()
connection.DisposeSocket()
imap.DisposeImap()
return
}
success = imap.Login(mailbox,auth.AccessToken())
if success != true {
imap.Disconnect()
fmt.Println("IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.")
key.DisposeJsonObject()
auth.DisposeAuthGoogle()
connection.DisposeSocket()
imap.DisposeImap()
return
}
folders := chilkat.NewMailboxes()
success = imap.MbxList(false,"","*",folders)
if success != true {
imap.Logout()
imap.Disconnect()
fmt.Println("Could not list Gmail folders.")
key.DisposeJsonObject()
auth.DisposeAuthGoogle()
connection.DisposeSocket()
imap.DisposeImap()
folders.DisposeMailboxes()
return
}
// List the labels visible through ordinary full-mail IMAP access.
i := 0
for (i < folders.Count()) {
fmt.Println(*folders.GetName(i))
i = i + 1
}
success = imap.ExamineMailbox("INBOX")
if success != true {
imap.Logout()
imap.Disconnect()
fmt.Println("Could not examine the impersonated user's INBOX.")
key.DisposeJsonObject()
auth.DisposeAuthGoogle()
connection.DisposeSocket()
imap.DisposeImap()
folders.DisposeMailboxes()
return
}
fmt.Println("INBOX messages: ", imap.NumMessages())
imap.Logout()
imap.Disconnect()
key.DisposeJsonObject()
auth.DisposeAuthGoogle()
connection.DisposeSocket()
imap.DisposeImap()
folders.DisposeMailboxes()