Swift
Swift
Calendar: Refresh Expired OAuth2 Access Token
See more Microsoft Calendar Examples
Refreshes an expired OAuth2 access token.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let json = CkoJsonObject()!
success = json.loadFile(path: "qa_data/tokens/msGraphCalendar.json")
if success != true {
return
}
let req = CkoHttpRequest()!
req.addParam(name: "grant_type", value: "refresh_token")
req.addParam(name: "redirect_uri", value: "http://localhost:3017/")
req.addParam(name: "client_id", value: "MICROSOFT-GRAPH-CLIENT-ID")
req.addParam(name: "client_secret", value: "MICROSOFT-GRAPH-CLIENT-SECRET")
req.addParam(name: "refresh_token", value: json.string(of: "refresh_token"))
req.addParam(name: "scope", value: "openid profile offline_access user.readwrite calendars.readwrite files.readwrite")
let http = CkoHttp()!
req.httpVerb = "POST"
req.contentType = "application/x-www-form-urlencoded"
let resp = CkoHttpResponse()!
success = http.httpReq(url: "https://login.microsoftonline.com/common/oauth2/v2.0/token", request: req, response: resp)
if success == false {
print("\(http.lastErrorText!)")
return
}
// Load the JSON response.
json.load(json: resp.bodyStr)
json.emitCompact = false
// Show the JSON response.
print("\(json.emit()!)")
print("Response status code: \(resp.statusCode.intValue)")
// If the response status code is not 200, then it's an error.
if resp.statusCode.intValue != 200 {
return
}
// Save the refreshed access token JSON to a file for future requests.
let fac = CkoFileAccess()!
fac.writeEntireTextFile(path: "qa_data/tokens/msGraphCalendar.json", fileData: json.emit(), charset: "utf-8", includePreamble: false)
print("Success.")
}