Sample code for 30+ languages & platforms
Tcl 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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

set key [new_CkJsonObject]

set success [CkJsonObject_LoadFile $key "gmail-service-account.json"]
if {$success != 1} then {
    puts "Could not load the service-account key from the private directory."
    delete_CkJsonObject $key
    exit
}

set auth [new_CkAuthGoogle]

CkAuthGoogle_put_JsonKey $auth [CkJsonObject_emit $key]
CkAuthGoogle_put_Scope $auth "https://www.googleapis.com/auth/gmail.metadata"
CkAuthGoogle_put_SubEmailAddress $auth "user@example.com"
CkAuthGoogle_put_ExpireNumSeconds $auth 3600

#  Obtain a token for the named Workspace user. Keep the computer clock accurate.
set connection [new_CkSocket]

CkSocket_put_RequireSslCertVerify $connection 1
set success [CkSocket_Connect $connection "www.googleapis.com" 443 1 10000]
if {$success != 1} then {
    puts "Could not establish the Google token connection."
    delete_CkJsonObject $key
    delete_CkAuthGoogle $auth
    delete_CkSocket $connection
    exit
}

set success [CkAuthGoogle_ObtainAccessToken $auth $connection]
CkSocket_Close $connection 1000
if {$success != 1} then {
    puts "Token exchange failed. Check the key, delegated scopes, user, and computer clock."
    delete_CkJsonObject $key
    delete_CkAuthGoogle $auth
    delete_CkSocket $connection
    exit
}

set http [new_CkHttp]

CkHttp_put_RequireSslCertVerify $http 1
CkHttp_put_AuthToken $http [CkAuthGoogle_accessToken $auth]
set resp [new_CkHttpResponse]

set reply [new_CkJsonObject]

set success [CkHttp_HttpNoBody $http "GET" "https://gmail.googleapis.com/gmail/v1/users/me/profile" $resp]
if {[CkHttpResponse_get_StatusCode $resp] == 0} then {
    puts "Gmail did not return an HTTP response."
    delete_CkJsonObject $key
    delete_CkAuthGoogle $auth
    delete_CkSocket $connection
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $reply
    exit
}

if {expr [$success != 1]  ||  [expr [[CkHttpResponse_get_StatusCode $resp] < 200]  ||  [[CkHttpResponse_get_StatusCode $resp] >= 300]]} then {
    puts "Gmail HTTP [CkHttpResponse_get_StatusCode $resp]"
    set success [CkJsonObject_Load $reply [CkHttpResponse_bodyStr $resp]]
    if {$success == 1} then {
        puts [CkJsonObject_stringOf $reply error.message]
    }

    delete_CkJsonObject $key
    delete_CkAuthGoogle $auth
    delete_CkSocket $connection
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $reply
    exit
}

set success [CkJsonObject_Load $reply [CkHttpResponse_bodyStr $resp]]
if {$success != 1} then {
    puts "Expected a Gmail JSON response."
    delete_CkJsonObject $key
    delete_CkAuthGoogle $auth
    delete_CkSocket $connection
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $reply
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $reply "emailAddress"] != 1]  ||  [[CkJsonObject_StringOfEquals $reply "emailAddress" "" 1] == 1]} then {
    puts "Expected emailAddress in the response."
    delete_CkJsonObject $key
    delete_CkAuthGoogle $auth
    delete_CkSocket $connection
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $reply
    exit
}

puts "Authorized mailbox: [CkJsonObject_stringOf $reply emailAddress]"
puts "Messages: [CkJsonObject_IntOf $reply messagesTotal]"

delete_CkJsonObject $key
delete_CkAuthGoogle $auth
delete_CkSocket $connection
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $reply