Lua
Lua
ETrade List Accounts
See more ETrade Examples
Returns a list of E*TRADE accounts for the current user.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 http = chilkat.Http.new()
http.oauth1 = true
http.oauth_verifier = ""
http.oauth_consumer_key = "ETRADE_CONSUMER_KEY"
http.oauth_consumer_secret = "ETRADE_CONSUMER_SECRET"
-- Load the access token previously obtained via the OAuth1 Authorization
local jsonToken = chilkat.JsonObject.new()
local success = jsonToken:load_file("qa_data/tokens/etrade.json")
if not success then
print("Failed to load OAuth1 token")
return
end
http.oauth_token = jsonToken:string_of("oauth_token")
http.oauth_token_secret = jsonToken:string_of("oauth_token_secret")
local sandboxUrl = "https://apisb.etrade.com/v1/accounts/list"
local liveUrl = "https://api.etrade.com/v1/accounts/list"
local resp = chilkat.HttpResponse.new()
success = http:http_no_body("GET", sandboxUrl, resp)
if not success then
print(http.last_error_text)
return
end
-- Make sure a successful response was received.
if resp.status_code >= 300 then
print(resp.status_line)
print(resp.header)
print(resp.body_str)
return
end
if resp.status_code == 204 then
print("No records available.")
return
end
-- Sample XML response:
-- Use this online tool to generate parsing code from sample XML:
-- Generate Parsing Code from XML
-- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
-- <AccountListResponse>
-- <Accounts>
-- <Account>
-- <accountId>82314598</accountId>
-- <accountIdKey>dBZOKt9xDrtRSAOl4MSiiA</accountIdKey>
-- <accountMode>IRA</accountMode>
-- <accountDesc>Brokerage</accountDesc>
-- <accountName>NickName-1</accountName>
-- <accountType>MARGIN</accountType>
-- <institutionType>BROKERAGE</institutionType>
-- <accountStatus>ACTIVE</accountStatus>
-- <closedDate>0</closedDate>
-- </Account>
-- <Account>
-- <accountId>58315636</accountId>
-- <accountIdKey>vQMsebA1H5WltUfDkJP48g</accountIdKey>
-- <accountMode>BROKERAGE</accountMode>
-- <accountDesc>Complete Savings</accountDesc>
-- <accountName>NickName-2</accountName>
-- <accountType>INDIVIDUAL</accountType>
-- <institutionType>BROKERAGE</institutionType>
-- <accountStatus>ACTIVE</accountStatus>
-- <closedDate>0</closedDate>
-- </Account>
-- <Account>
-- <accountId>70700418</accountId>
-- <accountIdKey>6_Dpy0rmuQ9cu9IbTfvF2A</accountIdKey>
-- <accountMode>CASH</accountMode>
-- <accountDesc>INDIVIDUAL</accountDesc>
-- <accountName>NickName-3</accountName>
-- <accountType>INDIVIDUAL</accountType>
-- <institutionType>BROKERAGE</institutionType>
-- <accountStatus>ACTIVE</accountStatus>
-- <closedDate>0</closedDate>
-- </Account>
-- <Account>
-- <accountId>83515143</accountId>
-- <accountIdKey>xj1Dc18FTqWPqkEEVUr5rw</accountIdKey>
-- <accountMode>CASH</accountMode>
-- <accountDesc>INDIVIDUAL</accountDesc>
-- <accountName/>
-- <accountType>CASH</accountType>
-- <institutionType>BROKERAGE</institutionType>
-- <accountStatus>CLOSED</accountStatus>
-- <closedDate>1521027780</closedDate>
--
-- </Account>
-- </Accounts>
-- </AccountListResponse>
local xml = chilkat.Xml.new()
xml:load_xml(resp.body_str)
print(xml:get_xml())
local accountId = 0
local accountIdKey = ""
local accountMode = ""
local accountDesc = ""
local accountName = ""
local accountType = ""
local institutionType = ""
local accountStatus = ""
local closedDate = 0
local i = 0
local count_i = xml:num_children_having_tag("Accounts|Account")
while i < count_i do
xml.i = i
accountId = xml:get_child_int_value("Accounts|Account[i]|accountId")
accountIdKey = xml:get_child_content("Accounts|Account[i]|accountIdKey")
accountMode = xml:get_child_content("Accounts|Account[i]|accountMode")
accountDesc = xml:get_child_content("Accounts|Account[i]|accountDesc")
accountName = xml:get_child_content("Accounts|Account[i]|accountName")
accountType = xml:get_child_content("Accounts|Account[i]|accountType")
institutionType = xml:get_child_content("Accounts|Account[i]|institutionType")
accountStatus = xml:get_child_content("Accounts|Account[i]|accountStatus")
closedDate = xml:get_child_int_value("Accounts|Account[i]|closedDate")
i = i + 1
end
print("Success.")