Lua
Lua
Refresh WiX Access Token
See more WiX Examples
Request a new access token each time you call a WiX API. Use the refresh token together with your secret key, to request refresh tokensChilkat Lua Downloads
local chilkat = require("chilkat")
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.Http.new()
-- Implements the following CURL command:
-- curl -X POST \
-- https://www.wix.com/oauth/access \
-- -H 'Content-Type: application/json' \
-- -d '{
-- "grant_type": "refresh_token",
-- "client_id": <CLIENT_ID>,
-- "client_secret": <CLIENT_SECRET>,
-- "refresh_token": <REFRESH_TOKEN>
-- }'
-- It is assumed we previously obtained an OAuth2 access token.
-- This example loads the JSON access token file
-- saved by this example: Get WiX OAuth2 Access Token
local jsonToken = chilkat.JsonObject.new()
local success = jsonToken:load_file("qa_data/tokens/wix.json")
if not success then
print("Failed to load square.json")
return
end
-- Get the "refresh_token"
local refreshToken = jsonToken:string_of("refresh_token")
-- The following JSON is sent in the request body.
-- {
-- "grant_type": "refresh_token",
-- "client_id": <APP_ID>,
-- "client_secret": <APP_SECRET>,
-- "refresh_token": <REFRESH_TOKEN>
-- }
local json = chilkat.JsonObject.new()
json:update_string("grant_type", "refresh_token")
json:update_string("client_id", "CLIENT_ID")
json:update_string("client_secret", "CLIENT_SECRET")
json:update_string("refresh_token", refreshToken)
local resp = chilkat.HttpResponse.new()
success = http:http_json("POST", "https://www.wix.com/oauth/access", json, "application/json", resp)
if not success then
print(http.last_error_text)
return
end
local sbResponseBody = chilkat.StringBuilder.new()
resp:get_body_sb(sbResponseBody)
local jResp = chilkat.JsonObject.new()
jResp:load_sb(sbResponseBody)
jResp.emit_compact = false
print("Response Body:")
print(jResp:emit())
local respStatusCode = resp.status_code
print("Response Status Code = " .. respStatusCode)
if respStatusCode >= 400 then
print("Response Header:")
print(resp.header)
print("Failed.")
return
end
-- Sample JSON response:
-- {
-- "refresh_token": "OAUTH2.eyJraWQ ... vnB4cQ",
-- "access_token": "OAUTH2.eyJra ... la18lrw"
-- }
local refresh_token = jResp:string_of("refresh_token")
local access_token = jResp:string_of("access_token")
-- Save the new JSON access token response to a file.
sbResponseBody:write_file("qa_data/tokens/wix.json", "utf-8", false)