Python TDAmeritrade API获取用户位置

Python TDAmeritrade API获取用户位置,python,json,Python,Json,我正在构建软件来跟踪我的投资组合和交易。我目前正试图从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'

我正在构建软件来跟踪我的投资组合和交易。我目前正试图从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)
上面的代码返回以下错误:

json.decoder.JSONDecodeError:期望值:第2行第11列(字符11)

更新:
我删除了包含
json.loads()
的行,因为它返回了一个401错误[一条错误消息指示调用方必须在HTTP授权请求头中传递一个有效的AuthToken]。我必须错误地传递
字段
apikey
参数。语法如何正确地传递这些参数?

您的参数看起来是正确的,但我认为url格式并不是您想要的。相反,您可以使用f字串格式化url,如下所示:

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

但是,已使用的f字符串仍返回401错误。这意味着它没有收到我的api/授权密钥。我如何解决这个问题?@BrianCos查看TD Ameritrade API文档,看起来这使用了一个承载令牌。在此调用之前,您是否通过Post访问令牌API获取承载令牌?来源:账户:无记名代币: