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

OneDrive: Authorize with OAuth2 and PKCE

See more OneDrive Examples

Register a Microsoft Entra application for the intended personal Microsoft accounts and/or Microsoft 365 work/school accounts. The current Microsoft identity platform v2 endpoints serve both; the old distinction between separate providers is obsolete. Add http://localhost:3017/ as a Mobile and desktop applications redirect URI. Chilkat receives the loopback callback, so no hosted web server is required.

A public client uses PKCE (S256) without a client secret. Use common, consumers, organizations, or a tenant-specific authority consistently for both endpoints according to your app registration. Request offline_access and the file scopes required by the application; this example uses Files.ReadWrite. It does not request unrelated profile-write permission.

The private qa_data/tokens/oneDrive.json cache contains access_token, refresh_token, expires_in, and obtained_at. Refresh before expiration with the same client ID/authority, save rotated refresh tokens, and handle revocation or interaction requirements with a new sign-in. Tokens are not printed. Use protected, atomic storage per app/account in production. Web and mobile apps need redirect handling suited to their environment.

Permission: Delegated Files.ReadWrite; offline_access for refresh.

Check the expected HTTP status before using a response body. A false method result can still contain an HTTP error response; status zero means no response arrived. For 401, renew authorization; for 403, check permissions, access grants, and policy; for 429, honor Retry-After with a bounded retry. Reconcile uncertain writes before repeating them. Keep credentials, file contents, upload/download URLs, and detailed diagnostics private.

Microsoft documentation

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
set success 0
set oauth [new_CkOAuth2]

#  Register a Mobile and desktop applications redirect URI: http://localhost:3017/
#  Use an account type that matches your app registration (common, consumers, or tenant ID).
CkOAuth2_put_AuthorizationEndpoint $oauth "https://login.microsoftonline.com/common/oauth2/v2.0/authorize"
CkOAuth2_put_TokenEndpoint $oauth "https://login.microsoftonline.com/common/oauth2/v2.0/token"
CkOAuth2_put_ClientId $oauth "YOUR_APPLICATION_CLIENT_ID"
#  Public desktop applications cannot keep a client secret. Use PKCE instead.
CkOAuth2_put_LocalHost $oauth "localhost"
CkOAuth2_put_ListenPort $oauth 3017
CkOAuth2_put_CodeChallenge $oauth 1
CkOAuth2_put_CodeChallengeMethod $oauth "S256"
#  Files.ReadWrite covers this collection. Use Files.Read for read-only operations.
CkOAuth2_put_Scope $oauth "offline_access https://graph.microsoft.com/Files.ReadWrite"
set success [CkOAuth2_AddAuthQueryParam $oauth "prompt" "select_account"]
if {$success != 1} then {
    puts "Could not configure the authorization request."
    delete_CkOAuth2 $oauth
    exit
}

set authUrl [CkOAuth2_startAuth $oauth]
if {[CkOAuth2_get_LastMethodSuccess $oauth] != 1} then {
    puts "Could not start the local authorization 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]

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

if {expr [[CkJsonObject_JsonTypeOf $token "access_token"] != 1]  ||  [[CkJsonObject_StringOfEquals $token "access_token" "" 1] == 1]} then {
    puts "Expected a nonempty access_token in the JSON."
    delete_CkOAuth2 $oauth
    delete_CkJsonObject $token
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $token "refresh_token"] != 1]  ||  [[CkJsonObject_StringOfEquals $token "refresh_token" "" 1] == 1]} then {
    puts "Expected a nonempty refresh_token in the JSON."
    delete_CkOAuth2 $oauth
    delete_CkJsonObject $token
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $token "expires_in"] != 2]  ||  [[CkJsonObject_IntOf $token "expires_in"] <= 0]} then {
    puts "Expected a positive access-token lifetime."
    delete_CkOAuth2 $oauth
    delete_CkJsonObject $token
    exit
}

set obtained [new_CkDateTime]

set success [CkDateTime_SetFromCurrentSystemTime $obtained]
if {$success != 1} then {
    puts "Could not record the token acquisition time."
    delete_CkOAuth2 $oauth
    delete_CkJsonObject $token
    delete_CkDateTime $obtained
    exit
}

CkJsonObject_UpdateString $token "obtained_at" [CkDateTime_getAsTimestamp $obtained 0]
#  This file contains credentials. Choose an access-controlled directory for the signed-in account.
set fac [new_CkFileAccess]

set success [CkFileAccess_DirEnsureExists $fac "qa_data/tokens"]
if {$success != 1} then {
    puts "Could not create the private token-cache directory."
    delete_CkOAuth2 $oauth
    delete_CkJsonObject $token
    delete_CkDateTime $obtained
    delete_CkFileAccess $fac
    exit
}

set success [CkJsonObject_WriteFile $token "qa_data/tokens/oneDrive.json"]
if {$success != 1} then {
    puts "Could not save the token cache."
    delete_CkOAuth2 $oauth
    delete_CkJsonObject $token
    delete_CkDateTime $obtained
    delete_CkFileAccess $fac
    exit
}

#  Never print access_token, refresh_token, or the complete token response.
puts "OneDrive authorization completed; token cache saved."

delete_CkOAuth2 $oauth
delete_CkJsonObject $token
delete_CkDateTime $obtained
delete_CkFileAccess $fac