Lua
Lua
UPS OAuth2 Client Credentials
See more UPS Examples
Get an OAuth2 access token for the UPS REST API using the client credentials flow (no interactivity with a web browser required).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.
local http = chilkat.Http.new()
-- Implements the following CURL command:
-- curl -i -X POST \
-- -u 2498righ8wr6aihe98rt8rhowirtyw9er6twe80rtywrehrt:nerf254578uh8rgt7y3h57358ouyth387h8h53h6yyh80hh578per9y7u5ruyuy4 \
-- https://wwwcie.ups.com/security/v1/oauth/token \
-- -H 'Content-Type: application/x-www-form-urlencoded' \
-- -H 'x-merchant-id: 7B3027' \
-- -d grant_type=client_credentials
-- Use the following online tool to generate HTTP code from a CURL command
-- Convert a cURL Command to HTTP Source Code
http.basic_auth = true
http.login = "2498righ8wr6aihe98rt8rhowirtyw9er6twe80rtywrehrt"
http.password = "nerf254578uh8rgt7y3h57358ouyth387h8h53h6yyh80hh578per9y7u5ruyuy4"
local req = chilkat.HttpRequest.new()
req:add_param("grant_type", "client_credentials")
req:add_header("x-merchant-id", "7B3027")
req.http_verb = "POST"
req.content_type = "application/x-www-form-urlencoded"
local resp = chilkat.HttpResponse.new()
local success = http:http_req("https://wwwcie.ups.com/security/v1/oauth/token", req, 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
-- Save the OAuth2 access token for other examples to use.
jResp:write_file("qa_data/tokens/ups_oauth2_token.json")
-- If successful, the OAuth2 access token JSON looks like this:
-- {
-- "token_type": "Bearer",
-- "issued_at": "1686911985606",
-- "client_id": "2498righ8wr6aihe98rt8rhowirtyw9er6twe80rtywrehrt",
-- "access_token": "eyJraW......R2sbqrY",
-- "expires_in": "14399",
-- "status": "approved"
-- }
local token_type = jResp:string_of("token_type")
local issued_at = jResp:string_of("issued_at")
local client_id = jResp:string_of("client_id")
local access_token = jResp:string_of("access_token")
local expires_in = jResp:string_of("expires_in")
local status = jResp:string_of("status")