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

Authorize a desktop application to use Gmail

See more GMail REST API Examples

Enable the Gmail API, configure the OAuth consent screen, and create a Desktop app OAuth client. Launch the system browser and receive the authorization code on a loopback listener. The sample requests read-only access with PKCE and offline access. For another operation, select the scope in that example before authorizing. Gmail scopes.

Run in a private, user-only directory. The file contains bearer credentials. In a deployed application use protected credential storage. To reuse an existing grant, copy the older examples’ qa_data/tokens/gmail.json to gmail-token.json in this private directory. The token retains its original scopes. Gmail clients using IMAP, POP, or SMTP with OAuth need the https://mail.google.com/ scope. Use a separate cache for each user and client.

Google documentation

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

local oauth = chilkat.OAuth2.new()
oauth.token_endpoint = "https://oauth2.googleapis.com/token"
oauth.client_id = "YOUR_DESKTOP_CLIENT_ID"
oauth.client_secret = "YOUR_DESKTOP_CLIENT_SECRET"

oauth.authorization_endpoint = "https://accounts.google.com/o/oauth2/v2/auth"
oauth.local_host = "127.0.0.1"
oauth.listen_port = 55568
oauth.listen_port_range_end = 55578
oauth.code_challenge = true
oauth.code_challenge_method = "S256"
oauth.scope = "https://www.googleapis.com/auth/gmail.readonly"
local success = oauth:add_auth_query_param("access_type", "offline")
if not success then
    print("Could not configure offline access.")
    return
end

-- This is the interactive setup step. Subsequent refreshes use the saved refresh token.
success = oauth:add_auth_query_param("prompt", "consent")
if not success then
    print("Could not configure consent.")
    return
end

local authUrl = oauth:start_auth()
if not oauth.last_method_success then
    print("Could not start the loopback listener.")
    return
end

success = oauth:launch_browser(authUrl)
if not success then
    oauth:cancel()
    print("Could not open the system browser.")
    return
end

local waited = 0
while (oauth.auth_flow_state < 3) and (waited < 180000) do
    oauth:sleep_ms(200)
    waited = waited + 200
end

if oauth.auth_flow_state < 3 then
    oauth:cancel()
    print("Authorization timed out.")
    return
end

if oauth.auth_flow_state ~= 3 then
    print("Authorization did not complete. Flow state: " .. oauth.auth_flow_state)
    return
end

local token = chilkat.JsonObject.new()
success = token:load(oauth.access_token_response)
if not success then
    print("Could not parse the token response.")
    return
end

if (token:json_type_of("refresh_token") ~= 1) or (token:string_of_equals("refresh_token", "", true)) then
    print("No offline refresh token was issued. The existing cache was preserved.")
    return
end

success = token:write_file("gmail-token.json")
if not success then
    print("Could not save the token file.")
    return
end

print("Credentials saved in the private working directory.")