Lua Requires Chilkat v9.5.0.65+
Lua
GeoOp Exchange Refresh Token for New Access Token
See more GeoOp Examples
Demonstrates how to use the /oauth2/token endpoint to exchange it for a new access token once the current access token has expired.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
-- This example also assumes that OAuth2 access and refresh tokens were previously fetched.
-- and saved in a JSON file.
-- First get our previously obtained refresh token.
-- { .... "refresh_token":"e6dqdG....mzjpT04w==", .... }
local jsonToken = chilkat.JsonObject.new()
local success = jsonToken:load_file("qa_data/tokens/geoop.json")
local rest = chilkat.Rest.new()
-- Connect to GeoOp...
local bAutoReconnect = true
success = rest:connect("login.geoop.com", 443, true, bAutoReconnect)
if not success then
print(rest.last_error_text)
return
end
-- Set the X-Version header.
rest:add_header("X-Version", "1.0")
-- Provide the required form params to get the new access token
print("refresh_token = " .. jsonToken:string_of("refresh_token"))
rest:add_query_param("refresh_token", jsonToken:string_of("refresh_token"))
rest:add_query_param("grant_type", "refresh_token")
rest:add_query_param("client_id", "GEOOP-CLIENT-ID")
rest:add_query_param("client_secret", "GEOOP-CLIENT-SECRET")
local responseBody = rest:full_request_form_url_encoded("POST", "/oauth2/token")
if not rest.last_method_success then
print(rest.last_error_text)
return
end
-- If the response status code did not indicate success, then see what happened..
if rest.response_status_code ~= 200 then
print("Request Header: ")
print(rest.last_request_header)
print("----")
print("Response StatusCode = " .. rest.response_status_code)
print("Response StatusLine: " .. rest.response_status_text)
print("Response Header:")
print(rest.response_header)
print(responseBody)
return
end
local json = chilkat.JsonObject.new()
json.emit_compact = false
json:load(responseBody)
-- Show the full JSON response. It should contain the new access token...
print(json:emit())