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

Visual Basic 6.0
Dim success As Long
success = 0

'  Assumes Chilkat is unlocked. See Global Unlock Sample.
success = 0
Dim token As New ChilkatJsonObject
token.Strict = 1
success = token.LoadFile("qa_data/tokens/outlookCalendar.json")
If (success <> 1) Then
    Debug.Print "Read the private token cache or authorize again."
    Exit Sub
End If

If ((token.JsonTypeOf("refresh_token") <> 1) Or (token.StringOfEquals("refresh_token","",1) = 1)) Then
    Debug.Print "Expected a nonempty refresh_token in the JSON."
    Exit Sub
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 ChilkatHttp
http.RequireSslCertVerify = 1
http.FollowRedirects = 0
http.ConnectTimeout = 20
http.ReadTimeout = 30
Dim req As New ChilkatHttpRequest
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 ChilkatHttpResponse
Dim result As New ChilkatJsonObject
result.Strict = 1
success = http.HttpReq(tokenUrl,req,resp)
'  A false result can still include an HTTP error response.
If ((success <> 1) And (resp.StatusCode = 0)) Then
    Debug.Print "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying."
    Exit Sub
End If

If (resp.StatusCode <> 200) Then
    Debug.Print "Unexpected HTTP status: " & resp.StatusCode
    Debug.Print "Request ID: " & resp.GetHeaderField("request-id")
    If (resp.StatusCode = 429) Then
        Debug.Print "Retry-After: " & resp.GetHeaderField("Retry-After")
    End If

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

    Exit Sub
End If

success = result.Load(resp.BodyStr)
If (success <> 1) Then
    Debug.Print "Expected a well-formed JSON response."
    Exit Sub
End If

If ((result.JsonTypeOf("access_token") <> 1) Or (result.StringOfEquals("access_token","",1) = 1)) Then
    Debug.Print "Expected a nonempty access_token in the JSON."
    Exit Sub
End If

If ((result.JsonTypeOf("expires_in") <> 2) Or (result.IntOf("expires_in") <= 0)) Then
    Debug.Print "Missing token lifetime; old cache unchanged."
    Exit Sub
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","",1) = 1)) Then
        Debug.Print "Expected a nonempty refresh_token in the JSON."
        Exit Sub
    End If

End If

Dim obtained As New CkDateTime
success = obtained.SetFromCurrentSystemTime()
If (success <> 1) Then
    Debug.Print "Could not timestamp the refreshed token."
    Exit Sub
End If

success = result.UpdateString("obtained_at",obtained.GetAsTimestamp(0))
'  Keep a valid previous cache for recovery if the replacement write fails.
success = token.WriteFile("qa_data/tokens/outlookCalendar.json.bak")
If (success <> 1) Then
    Debug.Print "Could not back up the old cache; no replacement attempted."
    Exit Sub
End If

success = result.WriteFile("qa_data/tokens/outlookCalendar.json")
If (success <> 1) Then
    Debug.Print "Could not save the new token; recover the previous cache from the private .bak file."
    Exit Sub
End If

Debug.Print "Refreshed the calendar token cache."