Lua
Lua
Configure OAuth 1.0a Authentication for REST
See more REST Examples
Demonstrates Rest.SetAuthOAuth1, which associates an OAuth1 provider so Chilkat computes the OAuth 1.0/1.0a signature for each request. The second argument selects whether OAuth parameters are placed in the query string or the Authorization header.
Background. OAuth 1.0a signs each request using the consumer key/secret and the access token/secret. Chilkat generates the nonce, timestamp, and signature automatically for every request sent through the object.
Chilkat Lua Downloads
local chilkat = require("chilkat")
local rest = chilkat.Rest.new()
local bTls = true
local bAutoReconnect = true
local success = rest:connect("example.com", 443, bTls, bAutoReconnect)
if not success then
print(rest.last_error_text)
return
end
-- Configure OAuth 1.0a request signing. Provide the consumer credentials and the access token
-- credentials; Chilkat computes the OAuth signature for each request.
local oauth1 = chilkat.OAuth1.new()
-- Normally you would not hard-code secrets in source. You should instead obtain them from an
-- interactive prompt, environment variable, or a secrets vault.
oauth1.consumer_key = "CONSUMER_KEY"
oauth1.consumer_secret = "CONSUMER_SECRET"
oauth1.token = "ACCESS_TOKEN"
oauth1.token_secret = "ACCESS_TOKEN_SECRET"
oauth1.signature_method = "HMAC-SHA1"
-- The 2nd argument selects whether OAuth parameters are placed in the query string (true) or the
-- Authorization header (false).
local bUseQueryParams = false
success = rest:set_auth_oauth1(oauth1, bUseQueryParams)
if not success then
print(rest.last_error_text)
return
end
local responseText = rest:full_request_no_body("GET", "/api/resource")
if not rest.last_method_success then
print(rest.last_error_text)
return
end
print(responseText)