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

Outlook Calendar: Refresh a Delegated Access Token

See more Outlook Calendar Examples

Refresh the Graph token obtained by the public desktop PKCE example, without a client secret or browser. Use the same client ID and authority. This works after a user has signed in and consented; it is not app-only authentication. A refresh token is not permanent: invalid_grant or an interaction requirement means the user must authorize again.

Form parameters are encoded by Chilkat. HTTP/JSON/token-validation failures leave the old cache untouched. Save a new refresh token when one is returned; retain the previous one if the field is absent. A private backup protects against a replacement-write failure; production code should replace credentials atomically, serialize refreshes per account, and remove obsolete protected backups according to its retention policy. No credentials are printed.

The cache is qa_data/tokens/outlookCalendar.json. Refresh before its acquired-time plus expires_in lifetime has elapsed. Keep each cache associated with its original app and account.

HTTP methods can return false for an HTTP error as well as a connection failure. The code distinguishes those cases and checks the expected status before reading JSON. For 401, refresh the access token or authorize again; for 403, check permissions, mailbox access, and tenant policy. For 429, honor Retry-After before a bounded retry. Do not automatically repeat a write after an uncertain outcome. Keep detailed errors and calendar data 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/outlookCalendar.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/outlookCalendar.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/outlookCalendar.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 calendar token cache."

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