简体   繁体   English

TDAmeritrade API 获取用户位置

[英]TDAmeritrade API get user positions

I am building software to track my portfolio and trade from.我正在构建软件来跟踪我的投资组合和交易。 I am currently trying to get my account positions from the TD API.我目前正在尝试从 TD API 获取我的账户头寸。

acct_endpt = 'https://api.tdameritrade.com/v1/accounts/{accountId}'
full_url_acct = acct_endpt.format(accountId='accountId')

account = requests.get(url=full_url_acct,
                       params={'fields' : 'positions', 'apikey' : 'apikey'})

acct_content = json.loads(account.content)

My code above is returning me the following error:我上面的代码返回了以下错误:

json.decoder.JSONDecodeError: Expecting value: line 2 column 11 (char 11)

Update: I removed the line containing json.loads() as it was returning a 401 error [An error message indicating the caller must pass a valid AuthToken in the HTTP authorization request header] .更新:我删除了包含json.loads()的行,因为它返回 401 错误[指示调用者必须在 HTTP 授权请求标头中传递有效 AuthToken 的错误消息] I must be passing the fields and apikey parameters incorrectly.我必须错误地传递fieldsapikey参数。 How would the syntax look to properly pass these parameters?语法如何正确传递这些参数?

Your parameters look correct, but I think the url formatting isn't doing what you want.您的参数看起来正确,但我认为 url 格式没有达到您想要的效果。 Instead, you can format your url using f-string as follows:相反,您可以使用 f-string 格式化 url,如下所示:

accountId = "ACCOUNT_ID"
acct_endpt = f"https://api.tdameritrade.com/v1/accounts/{accountId}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM