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

Calendar: Refresh Expired OAuth2 Access Token

See more Microsoft Calendar Examples

Refreshes an expired OAuth2 access token.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

local json = chilkat.JsonObject.new()
local success = json:load_file("qa_data/tokens/msGraphCalendar.json")
if not success then
    return
end

local req = chilkat.HttpRequest.new()
req:add_param("grant_type", "refresh_token")
req:add_param("redirect_uri", "http://localhost:3017/")
req:add_param("client_id", "MICROSOFT-GRAPH-CLIENT-ID")
req:add_param("client_secret", "MICROSOFT-GRAPH-CLIENT-SECRET")
req:add_param("refresh_token", json:string_of("refresh_token"))
req:add_param("scope", "openid profile offline_access user.readwrite calendars.readwrite files.readwrite")

local http = chilkat.Http.new()

req.http_verb = "POST"
req.content_type = "application/x-www-form-urlencoded"

local resp = chilkat.HttpResponse.new()
success = http:http_req("https://login.microsoftonline.com/common/oauth2/v2.0/token", req, resp)
if not success then
    print(http.last_error_text)
    return
end

-- Load the JSON response.
json:load(resp.body_str)
json.emit_compact = false

-- Show the JSON response.
print(json:emit())

print("Response status code: " .. resp.status_code)

-- If the response status code is not 200, then it's an error.
if resp.status_code ~= 200 then
    return
end

-- Save the refreshed access token JSON to a file for future requests.
local fac = chilkat.FileAccess.new()
fac:write_entire_text_file("qa_data/tokens/msGraphCalendar.json", json:emit(), "utf-8", false)

print("Success.")