I cannot understand how to properly use the query parameters using Etrade's production API. Regardless of the query parameter used, the response is always unauthorized. Here's the documentation relevant to this example. Most of the code is taken from the Python example on this page.
I receive a 200 response without the parameter and 401 when adding the parameter sortOrder=ASC
.
import configparser
from rauth import OAuth1Service
import webbrowser
# loading configuration file
config = configparser.ConfigParser()
config.read("etrade_python_client/config.ini")
etrade = OAuth1Service(
name="etrade",
consumer_key=config["DEFAULT"]["PROD_KEY"],
consumer_secret=config["DEFAULT"]["PROD_SECRET"],
request_token_url="https://api.etrade.com/oauth/request_token",
access_token_url="https://api.etrade.com/oauth/access_token",
authorize_url="https://us.etrade.com/e/t/etws/authorize?key={}&token={}",
base_url="https://api.etrade.com")
request_token, request_token_secret = etrade.get_request_token(params={"oauth_callback": "oob", "format": "json"})
authorize_url = etrade.authorize_url.format(etrade.consumer_key, request_token)
webbrowser.open(authorize_url)
The previous line opens a web browser which I navigate to and copy the code and store it as text_code
.
text_code = "<copy-code-from-web-browser>"
session = etrade.get_auth_session(request_token, request_token_secret, params={"oauth_verifier": text_code})
# get account info
url_account = "https://api.etrade.com/v1/accounts/list.json"
data_account = session.get(url_account, header_auth=True).json()
# store account id key
account_id_key = res["AccountListResponse"]["Accounts"]["Account"][0]["accountIdKey"]
# get portfolio and specify sortOrder
url_portfolio = "https://api.etrade.com/v1/accounts/{}/portfolio?sortOrder=ASC".format(account_id_key)
data_portfolio = session.get(url_portfolio, header_auth=True)
print(data_portfolio)
>>> <Response [401]>
# get portfolio and do not specify sort order
url_portfolio = "https://api.etrade.com/v1/accounts/{}/portfolio".format(account_id_key)
data_portfolio = session.get(url_portfolio, header_auth=True)
print(data_portfolio)
>>> <Response [200]>
Has anyone else ran into this issue? I'm thinking I must not be passing the query parameters correctly.