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

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.

Microsoft documentation

Chilkat Xojo Plugin Downloads

Xojo Plugin
Dim success As Boolean
success = False

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
success = False
Dim token As New Chilkat.JsonObject
token.Strict = True
success = token.LoadFile("qa_data/tokens/oneDrive.json")
If (success <> True) Then
    System.DebugLog("Read the private token cache or authorize again.")
    Return
End If

If ((token.JsonTypeOf("refresh_token") <> 1) Or (token.StringOfEquals("refresh_token","",True) = True)) Then
    System.DebugLog("Expected a nonempty refresh_token in the JSON.")
    Return
End If

Dim clientId As String
clientId = "YOUR_APPLICATION_CLIENT_ID"
//  Use the same app and authority as the original authorization.
Dim tokenUrl As String
tokenUrl = "https://login.microsoftonline.com/common/oauth2/v2.0/token"
Dim http As New Chilkat.Http
http.RequireSslCertVerify = True
http.FollowRedirects = False
http.ConnectTimeout = 20
http.ReadTimeout = 30
Dim req As New Chilkat.HttpRequest
req.HttpVerb = "POST"
req.ContentType = "application/x-www-form-urlencoded"
req.AddParam "client_id",clientId
req.AddParam "grant_type","refresh_token"
req.AddParam "refresh_token",token.StringOf("refresh_token")
//  Omitting scope retains the original grant instead of requesting additional permissions.
Dim resp As New Chilkat.HttpResponse
Dim result As New Chilkat.JsonObject
result.Strict = True
success = http.HttpReq(tokenUrl,req,resp)
//  A false result can still include an HTTP error response.
If ((success <> True) And (resp.StatusCode = 0)) Then
    System.DebugLog("No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying.")
    Return
End If

If (resp.StatusCode <> 200) Then
    System.DebugLog("Unexpected HTTP status: " + Str(resp.StatusCode))
    System.DebugLog("Request ID: " + resp.GetHeaderField("request-id"))
    If (resp.StatusCode = 429) Then
        System.DebugLog("Retry-After: " + resp.GetHeaderField("Retry-After"))
    End If

    If (resp.StatusCode = 401) Then
        System.DebugLog("Refresh the Graph access token or authorize again.")
    End If

    Return
End If

success = result.Load(resp.BodyStr)
If (success <> True) Then
    System.DebugLog("Expected a well-formed JSON response.")
    Return
End If

If ((result.JsonTypeOf("access_token") <> 1) Or (result.StringOfEquals("access_token","",True) = True)) Then
    System.DebugLog("Expected a nonempty access_token in the JSON.")
    Return
End If

If ((result.JsonTypeOf("expires_in") <> 2) Or (result.IntOf("expires_in") <= 0)) Then
    System.DebugLog("Missing token lifetime; old cache unchanged.")
    Return
End If

If (result.JsonTypeOf("refresh_token") = -1) Then
    success = result.UpdateString("refresh_token",token.StringOf("refresh_token"))
Else
    If ((result.JsonTypeOf("refresh_token") <> 1) Or (result.StringOfEquals("refresh_token","",True) = True)) Then
        System.DebugLog("Expected a nonempty refresh_token in the JSON.")
        Return
    End If

End If

Dim obtained As New Chilkat.CkDateTime
success = obtained.SetFromCurrentSystemTime()
If (success <> True) Then
    System.DebugLog("Could not timestamp the refreshed token.")
    Return
End If

success = result.UpdateString("obtained_at",obtained.GetAsTimestamp(False))
//  Keep a valid previous cache for recovery if the replacement write fails.
success = token.WriteFile("qa_data/tokens/oneDrive.json.bak")
If (success <> True) Then
    System.DebugLog("Could not back up the old cache; no replacement attempted.")
    Return
End If

success = result.WriteFile("qa_data/tokens/oneDrive.json")
If (success <> True) Then
    System.DebugLog("Could not save the new token; recover the previous cache from the private .bak file.")
    Return
End If

System.DebugLog("Refreshed the OneDrive token cache.")