Lua
Lua
PayPal -- Get an OAuth 2.0 Access Token
See more PayPal Examples
Demonstrates how to send a request to get a PayPal OAuth2 access token. Sends an HTTP request equivalent to the following:curl https://api.sandbox.paypal.com/v1/oauth2/token \ -H "Accept: application/json" \ -H "Accept-Language: en_US" \ -u "Client-Id:Secret" \ -d "grant_type=client_credentials"
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local rest = chilkat.Rest.new()
-- Make the initial connection.
-- A single REST object, once connected, can be used for many PayPal REST API calls.
-- The auto-reconnect indicates that if the already-established HTTPS connection is closed,
-- then it will be automatically re-established as needed.
local bAutoReconnect = true
local success = rest:connect("api.sandbox.paypal.com", 443, true, bAutoReconnect)
if not success then
print(rest.last_error_text)
return
end
-- Duplicate this request:
-- curl https://api.sandbox.paypal.com/v1/oauth2/token \
-- -H "Accept: application/json" \
-- -H "Accept-Language: en_US" \
-- -u "Client-Id:Secret" \
-- -d "grant_type=client_credentials"
rest:add_header("Accept", "application/json")
rest:add_header("Accept-Language", "en_US")
-- For additional help on where to find your client ID and API secret, see PayPal Client_ID and API_Secret
rest:set_auth_basic("PAYPAL_REST_API_CLIENT_ID", "PAYPAL_REST_API_SECRET")
rest:add_query_param("grant_type", "client_credentials")
local responseStr = rest:full_request_form_url_encoded("POST", "/v1/oauth2/token")
if not rest.last_method_success then
print(rest.last_error_text)
return
end
print(rest.last_request_header)
-- A sample response:
-- {
-- "scope": "https://api.paypal.com/v1/payments/.* https://api.paypal.com/v1/vault/credit-card https://api.paypal.com/v1/vault/credit-card/.*",
-- "access_token": "EEwJ6tF9x5WCIZDYzyZGaz6Khbw7raYRIBV_WxVvgmsG",
-- "token_type": "Bearer",
-- "app_id": "APP-6XR95014BA15863X",
-- "expires_in": 28800
-- }
local json = chilkat.JsonObject.new()
json:load(responseStr)
json.emit_compact = false
-- Check the response status code. A 200 indicates success..
if rest.response_status_code ~= 200 then
print(json:emit())
print("Failed.")
return
end
-- Given that the access token expires in approx 8 hours,
-- let's record the date/time this token was created.
-- This will allow us to know beforehand if the token
-- is expired (and we can then fetch a new token).
local dateTime = chilkat.DateTime.new()
local bLocalTime = false
local dtNow = dateTime:get_as_unix_time(bLocalTime)
json:append_int("tokenCreateTimeUtc", dtNow)
-- Examine the access token and save to a file.
print("Access Token: " .. json:string_of("access_token"))
print("Full JSON Response:")
print(json:emit())
local sbResponse = chilkat.StringBuilder.new()
sbResponse:append(json:emit())
local bEmitBom = false
sbResponse:write_file("qa_data/tokens/paypal.json", "utf-8", bEmitBom)