'How to create a watchlist with python using the TD Ameritrade api?
I hope there is someone that can help me with this error i'm having problems with. I have seen many post here about this api and have read almost everything I can. I have not been able to find any code examples related to creating a watchlist for the TD Ameritrade api anywhere. I've watching videos and took pieces and parts from other programmers code off of GitHub and tried to piece together something else that would work or maybe use it to try to understand what's happening and i've had no luck. I'm trying create a watch list that I can add tickers to. Maybe i'm not understanding what 'Create Watchlist' actually does. But, I keep encountering the error 403 "You don't have permission to access this resource." I am authorized and I can get account information and get what watchlists I have already. But, I cannot seem to post to the watchlists. I have googled the crap out of this and have been lost trying to find a answer. I'm new to coding so I know I don't understand absolutely everything. It's very frustrating that td does not really provide a lot of information on their api in general, but even more frustrating that I seemingly cannot find how this code is supposed to look anywhere. How does one successfully create a watchlist with the td api in python? Any pointers or help is welcomed!
url = r"https://api.tdameritrade.com/v1/oauth2/token"
headers = {"Content-Type":"application/x-www-form-urlencoded"}
payload = {'grant_type': 'authorization_code',
'access_type': 'offline',
'code': parse_url,
'client_id':client_id,
'redirect_uri':'redirecturlhere'}
authReply = requests.post(url, headers = headers, data=payload)
decoded_content = authReply.json()
access_token = decoded_content['access_token']
headers = {'Authorization': "Bearer {}".format(access_token)}
endpoint = r"https://api.tdameritrade.com/v1/accounts/"
content = requests.get(url = endpoint, headers = headers)
data = content.json()
account_id = data[0]['securitiesAccount']['accountId']
endpoint = r"https://api.tdameritrade.com/v1/accounts/{accountnumber}/watchlists"
Using.format(account_id) above this line I get error 415 unsupported media type. So, I just have been filling in the account number for now.
payload = {"name": "List", "watchlistItems": 'IVR'}
headers = {'Authorization': "Bearer {}".format(access_token),
'Content-type':'application/json'}
content = requests.post(url = endpoint, headers = headers, data=payload)
print(content)
data = content.json()
Solution 1:[1]
The problem is that due to how the request library works, if you specify the "Content-type" to 'application/json' as you have, the request will no longer accept 'data' as a parameter and must instead use 'json'.
In other words, assuming all your tokens are valid, the change that you need is:
...same...
content = requests.post(url = endpoint, headers = headers, json=payload)
...same...
Hope this helps.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Teedub |