Lua Requires Chilkat v11.0.0+
Lua
ETrade OAuth1 Authorization (3-legged) Step 2
See more ETrade Examples
Demonstrates the final step in 3-legged OAuth1 authorization for the ETrade REST API. Example uses the OAuth1 verifier code that was copy-and-pasted from the browser in the 1st step. The end result of this final OAuth1 step is an access token that can be used to make ETrade REST API calls.See https://apisb.etrade.com/docs/api/authorization/get_access_token.html
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 consumerKey = "ETRADE_CONSUMER_KEY"
local consumerSecret = "ETRADE_CONSUMER_SECRET"
local requestTokenUrl = "https://apisb.etrade.com/oauth/request_token"
local authorizeUrl = "https://us.etrade.com/e/t/etws/authorize"
local accessTokenUrl = "https://apisb.etrade.com/oauth/access_token"
local http = chilkat.Http.new()
local success = true
http.oauth1 = true
http.oauth_consumer_key = consumerKey
http.oauth_consumer_secret = consumerSecret
http.oauth_callback = "oob"
local jsonRequestToken = chilkat.JsonObject.new()
success = jsonRequestToken:load_file("qa_data/tokens/etrade_request_token.json")
local requestToken = jsonRequestToken:string_of("oauth_token")
local requestTokenSecret = jsonRequestToken:string_of("oauth_token_secret")
-- ------------------------------------------------------------------------------
-- Exchange the OAuth Request Token for an OAuth Access Token.
http.oauth_token = requestToken
http.oauth_token_secret = requestTokenSecret
-- This is the verifier that was interactively copy-and-pasted from the browser back to our app.
http.oauth_verifier = "NJ07S"
-- Use the explicit string "INCLUDE_OAUTH_TOKEN" to tell Chilkat to include the "oauth_token" param in the Authorization header field
http.uncommon_options = "INCLUDE_OAUTH_TOKEN"
local resp = chilkat.HttpResponse.new()
success = http:http_no_body("GET", accessTokenUrl, resp)
if not success then
print(http.last_error_text)
return
end
-- Make sure a successful response was received.
if resp.status_code ~= 200 then
print(resp.status_line)
print(resp.header)
print(resp.body_str)
return
end
-- If successful, the resp.BodyStr contains something like this:
-- oauth_token=85123455-fF41296Bi3daM8eCo9Y5vZabcdxXpRv864plYPOjr&oauth_token_secret=afiYJOgabcdSfGae7BDvJVVTwys8fUGpra5guZxbmFBZo
print(resp.body_str)
local hashTab = chilkat.Hashtable.new()
hashTab:add_query_params(resp.body_str)
local accessToken = hashTab:lookup_str("oauth_token")
local accessTokenSecret = hashTab:lookup_str("oauth_token_secret")
-- The access token + secret is what should be saved and used for
-- subsequent REST API calls.
print("Access Token = " .. accessToken)
print("Access Token Secret = " .. accessTokenSecret)
-- Save this access token for future calls.
-- Just in case we need user_id and screen_name, save those also..
local json = chilkat.JsonObject.new()
json:append_string("oauth_token", accessToken)
json:append_string("oauth_token_secret", accessTokenSecret)
local fac = chilkat.FileAccess.new()
fac:write_entire_text_file("qa_data/tokens/etrade.json", json:emit(), "utf-8", false)
print("Success.")