【问题标题】:API access to trading platform using Python使用 Python 访问交易平台的 API
【发布时间】:2017-03-12 09:17:50
【问题描述】:

我是使用 API 和 Python 获取数据的新手。我想从我的交易平台中提取数据。他们提供了以下说明:

http://www.questrade.com/api/documentation/getting-started

我可以完成第 4 步并拥有访问令牌。我需要第 5 步的帮助。如何翻译此请求:

GET /v1/accounts HTTP/1.1
Host: https://api01.iq.questrade.com
Authorization: Bearer C3lTUKuNQrAAmSD/TPjuV/HI7aNrAwDp

进入 Python 代码?我试过了

import requests
r = requests.get('https://api01.iq.questrade.com/v1/accounts', headers={'Authorization': 'access_token myToken'})

读完后我试过了:python request with authentication (access_token)

任何帮助将不胜感激。谢谢。

【问题讨论】:

  • headers = {'Authorization': 'Bearer C3lTUKuNQrAAmSD/TPjuV/HI7aNrAwDp'}
  • 似乎不起作用。获取
  • 您应该用您的真实访问令牌替换上例中Bearer 之后的那个。但这是预期的标题格式。 401 错误为 Unauthorized,表示令牌无效。
  • 我确实替换了我的真实访问令牌。我会向交易平台查询为什么会发生这种情况...
  • 第 4 步是“登录”功能,您是否已将您从用户前端获得的令牌交换为响应中的 access_token 对象?它是否在此期间过期了,可能吗?

标签: python api


【解决方案1】:

正如您所指出的,在第 4 步之后,您应该收到如下访问令牌:

{
    “access_token”: ”C3lTUKuNQrAAmSD/TPjuV/HI7aNrAwDp”,
    “token_type”: ”Bearer”,
    “expires_in”: 300,
    “refresh_token”: ”aSBe7wAAdx88QTbwut0tiu3SYic3ox8F”,
    “api_server”: ”https://api01.iq.questrade.com”
}

要进行后续 API 调用,您需要按如下方式构建 URI:

uri = [api_server]/v1/[rest_operation]

e.g.
uri = "https://api01.iq.questrade.com/v1/time"

Note: Make sure you use the same [api_server] that you received in your json object from step 4, otherwise your calls will not work with the given access_token

接下来,按如下方式构建您的标题:

headers = {'Authorization': [token_type] + ' ' + [access_token]}

e.g.
headers = {'Authorization': 'Bearer C3lTUKuNQrAAmSD/TPjuV/HI7aNrAwDp'}

最后,按如下方式发出您的请求

r = requests.get(uri, headers=headers)
response = r.json()

希望这会有所帮助!

注意:您可以在 GitHub 上找到一个 Questrade API Python 包装器,它可以为您处理上述所有内容。 https://github.com/pcinat/QuestradeAPI_PythonWrapper

【讨论】:

    【解决方案2】:

    对彼得的回复有所改进(谢谢彼得!) 首先使用您从 QT 网站获得的令牌来获取 access_token 并获得分配的 api_server 来处理您的请求。

    # replace XXXXXXXX with the token given to you in your questrade account
    
    import requests
    
    r = requests.get('https://login.questrade.com/oauth2/token?grant_type=refresh_token&refresh_token=XXXXXXXX')
    
    access_token = str(r.json()['access_token'])
    refresh_token= str(r.json()['refresh_token']) # you will need this refresh_token to obtain another access_token when it expires
    api_server= str(r.json()['api_server'])
    token_type= str(r.json()['token_type'])
    api_server= str(r.json()['api_server'])
    expires_in = str(r.json()['expires_in'])
    
    # uri = api_server+'v1/'+[action] - let's try checking the server's time:
    uri = api_server+'v1/'+'time'
    headers = {'Authorization': token_type +' '+access_token}
    # will look sth like this
     
    #    headers will look sth like    {'Authorization': 'Bearer ix7rAhcXx83judEVUa8egpK2JqhPD2_z0'}
    #        uri will look sth like    'https://api05.iq.questrade.com/v1/time'
    
    # you can test now with
    r = requests.get(uri, headers=headers)
    response = r.json()
    print(response)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-09
      • 2013-04-11
      • 2012-09-06
      • 2014-03-06
      • 2017-11-01
      • 2013-06-14
      • 2021-05-06
      • 2011-05-14
      相关资源
      最近更新 更多