Xbase++ Requires Chilkat v11.0.0+
Xbase++
Group: Refresh OAuth2 Access Token
See more Microsoft Group Examples
Refreshes an expired or non-expired OAuth2 access token for the Microsoft Group REST API.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oJson
LOCAL oReq
LOCAL oHttp
LOCAL oResp
LOCAL oFac
nSuccess := 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oJson := CreateObject("Chilkat.JsonObject")
nSuccess := oJson:LoadFile("qa_data/tokens/msGraphGroup.json")
IF (nSuccess != 1)
oJson:destroy()
RETURN
ENDIF
oReq := CreateObject("Chilkat.HttpRequest")
oReq:AddParam("grant_type", "refresh_token")
oReq:AddParam("redirect_uri", "http://localhost:3017/")
oReq:AddParam("client_id", "MICROSOFT-GRAPH-CLIENT-ID")
oReq:AddParam("client_secret", "MICROSOFT-GRAPH-CLIENT-SECRET")
oReq:AddParam("refresh_token", oJson:StringOf("refresh_token"))
oReq:AddParam("scope", "openid profile offline_access user.readwrite group.readwrite.all files.readwrite")
oHttp := CreateObject("Chilkat.Http")
oReq:HttpVerb := "POST"
oReq:ContentType := "application/x-www-form-urlencoded"
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpReq("https://login.microsoftonline.com/common/oauth2/v2.0/token", oReq, oResp)
IF (nSuccess == 0)
? oHttp:LastErrorText
oJson:destroy()
oReq:destroy()
oHttp:destroy()
oResp:destroy()
RETURN
ENDIF
// Load the JSON response.
oJson:Load(oResp:BodyStr)
oJson:EmitCompact := 0
// Show the JSON response.
? oJson:Emit()
? "Response status code: " + Str(oResp:StatusCode)
// If the response status code is not 200, then it's an error.
IF (oResp:StatusCode != 200)
oJson:destroy()
oReq:destroy()
oHttp:destroy()
oResp:destroy()
RETURN
ENDIF
// Save the refreshed access token JSON to a file for future requests.
oFac := CreateObject("Chilkat.FileAccess")
oFac:WriteEntireTextFile("qa_data/tokens/msGraphGroup.json", oJson:Emit(), "utf-8", 0)
? "Success."
oJson:destroy()
oReq:destroy()
oHttp:destroy()
oResp:destroy()
oFac:destroy()