OneDrive: Refresh a Delegated Access Token
See more OneDrive Examples
Refresh the token from OneDrive authorization with PKCE, using the same application client ID and authority. This retains the user grant; it does not convert it to application access. Omitting scope retains the existing scopes. Obtain additional consent through authorization when needed.
The private oneDrive.json token cache is replaced only after a successful, validated response. Retain the previous refresh token when no replacement is returned, and save a rotated refresh token when supplied. A private backup is written first; production storage should use atomic replacement and a single refresh writer per account/application. invalid_grant or an interaction requirement requires sign-in again. Tokens and raw responses are not printed.
Permission: The original delegated grant.
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.
Chilkat AutoIt Downloads
Local $bSuccess = False
; Assumes Chilkat is unlocked. See Global Unlock Sample.
$bSuccess = False
$oToken = ObjCreate("Chilkat.JsonObject")
$oToken.Strict = True
$bSuccess = $oToken.LoadFile("qa_data/tokens/oneDrive.json")
If ($bSuccess <> True) Then
ConsoleWrite("Read the private token cache or authorize again." & @CRLF)
Exit
EndIf
If (($oToken.JsonTypeOf("refresh_token") <> 1) Or ($oToken.StringOfEquals("refresh_token","",True) = True)) Then
ConsoleWrite("Expected a nonempty refresh_token in the JSON." & @CRLF)
Exit
EndIf
Local $sClientId = "YOUR_APPLICATION_CLIENT_ID"
; Use the same app and authority as the original authorization.
Local $sTokenUrl = "https://login.microsoftonline.com/common/oauth2/v2.0/token"
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
$oHttp.FollowRedirects = False
$oHttp.ConnectTimeout = 20
$oHttp.ReadTimeout = 30
$oReq = ObjCreate("Chilkat.HttpRequest")
$oReq.HttpVerb = "POST"
$oReq.ContentType = "application/x-www-form-urlencoded"
$oReq.AddParam "client_id",$sClientId
$oReq.AddParam "grant_type","refresh_token"
$oReq.AddParam "refresh_token",$oToken.StringOf("refresh_token")
; Omitting scope retains the original grant instead of requesting additional permissions.
$oResp = ObjCreate("Chilkat.HttpResponse")
$oResult = ObjCreate("Chilkat.JsonObject")
$oResult.Strict = True
$bSuccess = $oHttp.HttpReq($sTokenUrl,$oReq,$oResp)
; A false result can still include an HTTP error response.
If (($bSuccess <> True) And ($oResp.StatusCode = 0)) Then
ConsoleWrite("No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying." & @CRLF)
Exit
EndIf
If ($oResp.StatusCode <> 200) Then
ConsoleWrite("Unexpected HTTP status: " & $oResp.StatusCode & @CRLF)
ConsoleWrite("Request ID: " & $oResp.GetHeaderField("request-id") & @CRLF)
If ($oResp.StatusCode = 429) Then
ConsoleWrite("Retry-After: " & $oResp.GetHeaderField("Retry-After") & @CRLF)
EndIf
If ($oResp.StatusCode = 401) Then
ConsoleWrite("Refresh the Graph access token or authorize again." & @CRLF)
EndIf
Exit
EndIf
$bSuccess = $oResult.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Expected a well-formed JSON response." & @CRLF)
Exit
EndIf
If (($oResult.JsonTypeOf("access_token") <> 1) Or ($oResult.StringOfEquals("access_token","",True) = True)) Then
ConsoleWrite("Expected a nonempty access_token in the JSON." & @CRLF)
Exit
EndIf
If (($oResult.JsonTypeOf("expires_in") <> 2) Or ($oResult.IntOf("expires_in") <= 0)) Then
ConsoleWrite("Missing token lifetime; old cache unchanged." & @CRLF)
Exit
EndIf
If ($oResult.JsonTypeOf("refresh_token") = -1) Then
$oResult.UpdateString("refresh_token",$oToken.StringOf("refresh_token"))
Else
If (($oResult.JsonTypeOf("refresh_token") <> 1) Or ($oResult.StringOfEquals("refresh_token","",True) = True)) Then
ConsoleWrite("Expected a nonempty refresh_token in the JSON." & @CRLF)
Exit
EndIf
EndIf
$oObtained = ObjCreate("Chilkat.CkDateTime")
$bSuccess = $oObtained.SetFromCurrentSystemTime()
If ($bSuccess <> True) Then
ConsoleWrite("Could not timestamp the refreshed token." & @CRLF)
Exit
EndIf
$oResult.UpdateString("obtained_at",$oObtained.GetAsTimestamp(False))
; Keep a valid previous cache for recovery if the replacement write fails.
$bSuccess = $oToken.WriteFile("qa_data/tokens/oneDrive.json.bak")
If ($bSuccess <> True) Then
ConsoleWrite("Could not back up the old cache; no replacement attempted." & @CRLF)
Exit
EndIf
$bSuccess = $oResult.WriteFile("qa_data/tokens/oneDrive.json")
If ($bSuccess <> True) Then
ConsoleWrite("Could not save the new token; recover the previous cache from the private .bak file." & @CRLF)
Exit
EndIf
ConsoleWrite("Refreshed the OneDrive token cache." & @CRLF)