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

Refresh a Gmail OAuth access token

See more GMail REST API Examples

Use the refresh token and the same Desktop app client that issued it. Run in the private directory used by the authorization example. Preserve the refresh token if Google omits it from the response. Refreshing continues the existing grant. Request additional scopes through interactive authorization.

Google documentation

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

set token [new_CkJsonObject]

set success [CkJsonObject_LoadFile $token "gmail-token.json"]
if {$success != 1} then {
    puts "Could not read gmail-token.json from the private working directory."
    delete_CkJsonObject $token
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $token "refresh_token"] != 1]  ||  [[CkJsonObject_StringOfEquals $token "refresh_token" "" 1] == 1]} then {
    puts "Authorize the account for offline access first."
    delete_CkJsonObject $token
    exit
}

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_RefreshToken $oauth [CkJsonObject_stringOf $token "refresh_token"]
set success [CkOAuth2_RefreshAccessToken $oauth]
if {$success != 1} then {
    puts "Refresh failed. Check the OAuth client and grant. A revoked or expired grant requires fresh consent."
    delete_CkJsonObject $token
    delete_CkOAuth2 $oauth
    exit
}

set fresh [new_CkJsonObject]

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

if {expr [[CkJsonObject_JsonTypeOf $fresh "access_token"] != 1]  ||  [[CkJsonObject_StringOfEquals $fresh "access_token" "" 1] == 1]} then {
    puts "The refresh response did not contain an access token."
    delete_CkJsonObject $token
    delete_CkOAuth2 $oauth
    delete_CkJsonObject $fresh
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $fresh "refresh_token"] != 1]  ||  [[CkJsonObject_StringOfEquals $fresh "refresh_token" "" 1] == 1]} then {
    CkJsonObject_UpdateString $fresh "refresh_token" [CkJsonObject_stringOf $token "refresh_token"]
}

if {expr [[CkJsonObject_HasMember $fresh "scope"] != 1]  &&  [[CkJsonObject_JsonTypeOf $token "scope"] == 1]} then {
    CkJsonObject_UpdateString $fresh "scope" [CkJsonObject_stringOf $token "scope"]
}

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

puts "Refreshed the access token."

delete_CkJsonObject $token
delete_CkOAuth2 $oauth
delete_CkJsonObject $fresh