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

Outlook.com: Refresh Mail OAuth2 Tokens

See more Office365 Examples

Exchange a saved delegated refresh token for a new access token without displaying a browser. Use the same app registration and authority as the original authorization. Save any rotated refresh token and the new lifetime information. On a rejected or malformed response, this example preserves the previous cache; an expired/revoked grant or interaction-required response needs interactive authorization again.

Read and update qa_data/tokens/hotmail.json in a private working directory. Serialize access to the cache; production storage should support atomic replacement and protect credentials. A successful refresh does not enable a disabled mail protocol. Client-credentials tokens have no refresh token; request a new application token instead.

Authorize first: desktop PKCE example. This is delegated background access after consent, not permanently unattended application authentication.

Microsoft reference 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 "qa_data/tokens/hotmail.json"]
if {$success != 1} then {
    puts "Could not read qa_data/tokens/hotmail.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://login.microsoftonline.com/consumers/oauth2/v2.0/token"
CkOAuth2_put_ClientId $oauth "CLIENT_ID"
#  No client secret for the public desktop registration used by the PKCE example.
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"]
}

#  Persist the rotated refresh token. Retain the previous one only if the response omits it.
#  Use the new expires_in, not the old response's expiration information.
set obtained [new_CkDateTime]

CkDateTime_SetFromCurrentSystemTime $obtained
CkJsonObject_UpdateString $fresh "obtained_at" [CkDateTime_getAsTimestamp $obtained 0]
set success [CkJsonObject_WriteFile $fresh "qa_data/tokens/hotmail.json"]
if {$success != 1} then {
    puts "Could not save the refreshed credentials."
    delete_CkJsonObject $token
    delete_CkOAuth2 $oauth
    delete_CkJsonObject $fresh
    delete_CkDateTime $obtained
    exit
}

puts "Refreshed the access token."

delete_CkJsonObject $token
delete_CkOAuth2 $oauth
delete_CkJsonObject $fresh
delete_CkDateTime $obtained