Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

Access a Google Workspace mailbox with a service account

See more GMail REST API Examples

A Google Workspace super administrator must authorize the service account client ID for https://www.googleapis.com/auth/gmail.metadata in Admin console domain-wide delegation. Enable the Gmail API in the Cloud project. Set SubEmailAddress to a Gmail-enabled user in that organization. The profile response identifies the mailbox represented by me.

Store the downloaded JSON service-account key in gmail-service-account.json in a private working directory. AuthGoogle signs a JWT with the key and exchanges it for an access token. Repeat that exchange when the token expires. For other operations, change Scope and grant that scope in the Admin console. This workflow is for organization-controlled applications. Personal Gmail accounts can use offline authorization with a refresh token.

Google documentation

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Assumes Chilkat is unlocked. See Global Unlock Sample.

local key = chilkat.JsonObject.new()
local success = key:load_file("gmail-service-account.json")
if not success then
    print("Could not load the service-account key from the private directory.")
    return
end

local auth = chilkat.AuthGoogle.new()
auth.json_key = key:emit()
auth.scope = "https://www.googleapis.com/auth/gmail.metadata"
auth.sub_email_address = "user@example.com"
auth.expire_num_seconds = 3600

-- Obtain a token for the named Workspace user. Keep the computer clock accurate.
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("Token exchange failed. Check the key, delegated scopes, user, and computer clock.")
    return
end

local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
http.auth_token = auth.access_token
local resp = chilkat.HttpResponse.new()
local reply = chilkat.JsonObject.new()
success = http:http_no_body("GET", "https://gmail.googleapis.com/gmail/v1/users/me/profile", resp)
if resp.status_code == 0 then
    print("Gmail did not return an HTTP response.")
    return
end

if (not success) or ((resp.status_code < 200) or (resp.status_code >= 300)) then
    print("Gmail HTTP " .. resp.status_code)
    success = reply:load(resp.body_str)
    if success then
        print(reply:string_of("error.message"))
    end

    return
end

success = reply:load(resp.body_str)
if not success then
    print("Expected a Gmail JSON response.")
    return
end

if (reply:json_type_of("emailAddress") ~= 1) or (reply:string_of_equals("emailAddress", "", true)) then
    print("Expected emailAddress in the response.")
    return
end

print("Authorized mailbox: " .. reply:string_of("emailAddress"))
print("Messages: " .. reply:int_of("messagesTotal"))