Sample code for 30+ languages & platforms
B4X Requires Chilkat v11.0.0+

Calendar: Refresh Expired OAuth2 Access Token

See more Microsoft Calendar Examples

Refreshes an expired OAuth2 access token.

Chilkat B4X Downloads

B4X
Dim success As Boolean = False

'  This example requires the Chilkat API to have been previously unlocked.
'  See Global Unlock Sample for sample code.

Dim json As ChilkatJsonObject
json.Initialize
success = json.LoadFile("qa_data/tokens/msGraphCalendar.json")
If success <> True Then
    Return
End If


Dim req As ChilkatHttpRequest
req.Initialize
req.AddParam("grant_type", "refresh_token")
req.AddParam("redirect_uri", "http://localhost:3017/")
req.AddParam("client_id", "MICROSOFT-GRAPH-CLIENT-ID")
req.AddParam("client_secret", "MICROSOFT-GRAPH-CLIENT-SECRET")
req.AddParam("refresh_token", json.StringOf("refresh_token"))
req.AddParam("scope", "openid profile offline_access user.readwrite calendars.readwrite files.readwrite")

Dim http As ChilkatHttp
http.Initialize("http")

req.HttpVerb = "POST"
req.ContentType = "application/x-www-form-urlencoded"

Dim resp As ChilkatHttpResponse
resp.Initialize
success = http.HttpReq("https://login.microsoftonline.com/common/oauth2/v2.0/token", req, resp)
If success = False Then
    Log(http.LastErrorText)
    Return
End If


'  Load the JSON response.
json.Load(resp.BodyStr)
json.EmitCompact = False

'  Show the JSON response.
Log(json.Emit)

Log("Response status code: " & resp.StatusCode)

'  If the response status code is not 200, then it's an error.
If resp.StatusCode <> 200 Then
    Return
End If


'  Save the refreshed access token JSON to a file for future requests.
Dim fac As ChilkatFileAccess
fac.Initialize
fac.WriteEntireTextFile("qa_data/tokens/msGraphCalendar.json", json.Emit, "utf-8", False)

Log("Success.")