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 Lua Downloads
local chilkat = require("chilkat")
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local mailbox = "user@example.com"
local key = chilkat.JsonObject.new()
local success = key:load_file("gmail-service-account.json")
if not success then
print("Could not load the private service-account key.")
return
end
if (not key:string_of_equals("type", "service_account", true)) or (key:json_type_of("private_key") ~= 1) then
print("Expected a Google service-account JSON key.")
return
end
local auth = chilkat.AuthGoogle.new()
auth.json_key = key:emit()
auth.sub_email_address = mailbox
auth.scope = "https://mail.google.com/"
auth.expire_num_seconds = 3600
local connection = chilkat.Socket.new()
connection.require_ssl_cert_verify = true
success = connection:connect("www.googleapis.com", 443, true, 10000)
if not success then
print("Could not establish the Google token connection.")
return
end
success = auth:obtain_access_token(connection)
connection:close(1000)
if not success then
print("Check the key, domain-wide delegation client ID/scope, user, and computer clock.")
return
end
local imap = chilkat.Imap.new()
imap.ssl = true
imap.port = 993
imap.require_ssl_cert_verify = true
imap.auth_method = "XOAUTH2"
success = imap:connect("imap.gmail.com")
if not success then
print("Could not establish the Gmail IMAP TLS connection.")
return
end
success = imap:login(mailbox, auth.access_token)
if not success then
imap:disconnect()
print("IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.")
return
end
local folders = chilkat.Mailboxes.new()
success = imap:mbx_list(false, "", "*", folders)
if not success then
imap:logout()
imap:disconnect()
print("Could not list Gmail folders.")
return
end
-- List the labels visible through ordinary full-mail IMAP access.
local i = 0
while i < folders.count do
print(folders:get_name(i))
i = i + 1
end
success = imap:examine_mailbox("INBOX")
if not success then
imap:logout()
imap:disconnect()
print("Could not examine the impersonated user's INBOX.")
return
end
print("INBOX messages: " .. imap.num_messages)
imap:logout()
imap:disconnect()