Sample code for 30+ languages & platforms
Visual FoxPro 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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loToken
LOCAL lcClientId
LOCAL lcTokenUrl
LOCAL loHttp
LOCAL loReq
LOCAL loResp
LOCAL loResult
LOCAL loObtained

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0
loToken = CreateObject('Chilkat.JsonObject')
loToken.Strict = 1
lnSuccess = loToken.LoadFile("qa_data/tokens/oneDrive.json")
IF (lnSuccess <> 1) THEN
    ? "Read the private token cache or authorize again."
    RELEASE loToken
    CANCEL
ENDIF

IF ((loToken.JsonTypeOf("refresh_token") <> 1) OR (loToken.StringOfEquals("refresh_token","",1) = 1)) THEN
    ? "Expected a nonempty refresh_token in the JSON."
    RELEASE loToken
    CANCEL
ENDIF

lcClientId = "YOUR_APPLICATION_CLIENT_ID"
*  Use the same app and authority as the original authorization.
lcTokenUrl = "https://login.microsoftonline.com/common/oauth2/v2.0/token"
loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
loHttp.FollowRedirects = 0
loHttp.ConnectTimeout = 20
loHttp.ReadTimeout = 30
loReq = CreateObject('Chilkat.HttpRequest')
loReq.HttpVerb = "POST"
loReq.ContentType = "application/x-www-form-urlencoded"
loReq.AddParam("client_id",lcClientId)
loReq.AddParam("grant_type","refresh_token")
loReq.AddParam("refresh_token",loToken.StringOf("refresh_token"))
*  Omitting scope retains the original grant instead of requesting additional permissions.
loResp = CreateObject('Chilkat.HttpResponse')
loResult = CreateObject('Chilkat.JsonObject')
loResult.Strict = 1
lnSuccess = loHttp.HttpReq(lcTokenUrl,loReq,loResp)
*  A false result can still include an HTTP error response.
IF ((lnSuccess <> 1) AND (loResp.StatusCode = 0)) THEN
    ? "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying."
    RELEASE loToken
    RELEASE loHttp
    RELEASE loReq
    RELEASE loResp
    RELEASE loResult
    CANCEL
ENDIF

IF (loResp.StatusCode <> 200) THEN
    ? "Unexpected HTTP status: " + STR(loResp.StatusCode)
    ? "Request ID: " + loResp.GetHeaderField("request-id")
    IF (loResp.StatusCode = 429) THEN
        ? "Retry-After: " + loResp.GetHeaderField("Retry-After")
    ENDIF

    IF (loResp.StatusCode = 401) THEN
        ? "Refresh the Graph access token or authorize again."
    ENDIF

    RELEASE loToken
    RELEASE loHttp
    RELEASE loReq
    RELEASE loResp
    RELEASE loResult
    CANCEL
ENDIF

lnSuccess = loResult.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
    ? "Expected a well-formed JSON response."
    RELEASE loToken
    RELEASE loHttp
    RELEASE loReq
    RELEASE loResp
    RELEASE loResult
    CANCEL
ENDIF

IF ((loResult.JsonTypeOf("access_token") <> 1) OR (loResult.StringOfEquals("access_token","",1) = 1)) THEN
    ? "Expected a nonempty access_token in the JSON."
    RELEASE loToken
    RELEASE loHttp
    RELEASE loReq
    RELEASE loResp
    RELEASE loResult
    CANCEL
ENDIF

IF ((loResult.JsonTypeOf("expires_in") <> 2) OR (loResult.IntOf("expires_in") <= 0)) THEN
    ? "Missing token lifetime; old cache unchanged."
    RELEASE loToken
    RELEASE loHttp
    RELEASE loReq
    RELEASE loResp
    RELEASE loResult
    CANCEL
ENDIF

IF (loResult.JsonTypeOf("refresh_token") = -1) THEN
    loResult.UpdateString("refresh_token",loToken.StringOf("refresh_token"))
ELSE
    IF ((loResult.JsonTypeOf("refresh_token") <> 1) OR (loResult.StringOfEquals("refresh_token","",1) = 1)) THEN
        ? "Expected a nonempty refresh_token in the JSON."
        RELEASE loToken
        RELEASE loHttp
        RELEASE loReq
        RELEASE loResp
        RELEASE loResult
        CANCEL
    ENDIF

ENDIF

loObtained = CreateObject('Chilkat.CkDateTime')
lnSuccess = loObtained.SetFromCurrentSystemTime()
IF (lnSuccess <> 1) THEN
    ? "Could not timestamp the refreshed token."
    RELEASE loToken
    RELEASE loHttp
    RELEASE loReq
    RELEASE loResp
    RELEASE loResult
    RELEASE loObtained
    CANCEL
ENDIF

loResult.UpdateString("obtained_at",loObtained.GetAsTimestamp(0))
*  Keep a valid previous cache for recovery if the replacement write fails.
lnSuccess = loToken.WriteFile("qa_data/tokens/oneDrive.json.bak")
IF (lnSuccess <> 1) THEN
    ? "Could not back up the old cache; no replacement attempted."
    RELEASE loToken
    RELEASE loHttp
    RELEASE loReq
    RELEASE loResp
    RELEASE loResult
    RELEASE loObtained
    CANCEL
ENDIF

lnSuccess = loResult.WriteFile("qa_data/tokens/oneDrive.json")
IF (lnSuccess <> 1) THEN
    ? "Could not save the new token; recover the previous cache from the private .bak file."
    RELEASE loToken
    RELEASE loHttp
    RELEASE loReq
    RELEASE loResp
    RELEASE loResult
    RELEASE loObtained
    CANCEL
ENDIF

? "Refreshed the OneDrive token cache."

RELEASE loToken
RELEASE loHttp
RELEASE loReq
RELEASE loResp
RELEASE loResult
RELEASE loObtained