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

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

set oauth [new_CkOAuth2]

CkOAuth2_put_TokenEndpoint $oauth "https://oauth2.googleapis.com/token"
CkOAuth2_put_ClientId $oauth "YOUR_DESKTOP_CLIENT_ID"
CkOAuth2_put_ClientSecret $oauth "YOUR_DESKTOP_CLIENT_SECRET"

CkOAuth2_put_AuthorizationEndpoint $oauth "https://accounts.google.com/o/oauth2/v2/auth"
CkOAuth2_put_LocalHost $oauth "127.0.0.1"
CkOAuth2_put_ListenPort $oauth 55568
CkOAuth2_put_ListenPortRangeEnd $oauth 55578
CkOAuth2_put_CodeChallenge $oauth 1
CkOAuth2_put_CodeChallengeMethod $oauth "S256"
CkOAuth2_put_Scope $oauth "https://www.googleapis.com/auth/gmail.readonly"
set success [CkOAuth2_AddAuthQueryParam $oauth "access_type" "offline"]
if {$success != 1} then {
    puts "Could not configure offline access."
    delete_CkOAuth2 $oauth
    exit
}

#  This is the interactive setup step. Subsequent refreshes use the saved refresh token.
set success [CkOAuth2_AddAuthQueryParam $oauth "prompt" "consent"]
if {$success != 1} then {
    puts "Could not configure consent."
    delete_CkOAuth2 $oauth
    exit
}

set authUrl [CkOAuth2_startAuth $oauth]
if {[CkOAuth2_get_LastMethodSuccess $oauth] != 1} then {
    puts "Could not start the loopback listener."
    delete_CkOAuth2 $oauth
    exit
}

set success [CkOAuth2_LaunchBrowser $oauth $authUrl]
if {$success != 1} then {
    CkOAuth2_Cancel $oauth
    puts "Could not open the system browser."
    delete_CkOAuth2 $oauth
    exit
}

set waited 0
while {expr [[CkOAuth2_get_AuthFlowState $oauth] < 3]  &&  [$waited < 180000]} {
    CkOAuth2_SleepMs $oauth 200
    set waited [expr $waited + 200]
}
if {[CkOAuth2_get_AuthFlowState $oauth] < 3} then {
    CkOAuth2_Cancel $oauth
    puts "Authorization timed out."
    delete_CkOAuth2 $oauth
    exit
}

if {[CkOAuth2_get_AuthFlowState $oauth] != 3} then {
    puts "Authorization did not complete. Flow state: [CkOAuth2_get_AuthFlowState $oauth]"
    delete_CkOAuth2 $oauth
    exit
}

set token [new_CkJsonObject]

set success [CkJsonObject_Load $token [CkOAuth2_accessTokenResponse $oauth]]
if {$success != 1} then {
    puts "Could not parse the token response."
    delete_CkOAuth2 $oauth
    delete_CkJsonObject $token
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $token "refresh_token"] != 1]  ||  [[CkJsonObject_StringOfEquals $token "refresh_token" "" 1] == 1]} then {
    puts "No offline refresh token was issued. The existing cache was preserved."
    delete_CkOAuth2 $oauth
    delete_CkJsonObject $token
    exit
}

set success [CkJsonObject_WriteFile $token "gmail-token.json"]
if {$success != 1} then {
    puts "Could not save the token file."
    delete_CkOAuth2 $oauth
    delete_CkJsonObject $token
    exit
}

puts "Credentials saved in the private working directory."

delete_CkOAuth2 $oauth
delete_CkJsonObject $token