Outlook Contacts: Refresh a Delegated Access Token
See more Outlook Contact Examples
After interactive authorization, refresh the Graph access token without reopening a browser. Use the same app client ID and authority as the contacts OAuth2 example. Omitting scope retains the original grant. This is delegated access, not application authentication; invalid_grant or an interaction requirement needs fresh interactive authorization.
The example validates the response, retains the previous refresh token when no replacement is returned, and saves a rotated token when present. HTTP or validation failures leave the old cache unchanged. A private backup protects against replacement-write failure; production applications should use protected atomic credential storage and serialize refreshes per account. Tokens are never printed.
Uses qa_data/tokens/outlookContacts.json. Refresh before the acquired time plus expires_in lifetime elapses. Keep the 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 contact data private.
Chilkat Visual FoxPro Downloads
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/outlookContacts.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/outlookContacts.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/outlookContacts.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 contacts token cache."
RELEASE loToken
RELEASE loHttp
RELEASE loReq
RELEASE loResp
RELEASE loResult
RELEASE loObtained