Skip to main content
 首页 » 编程设计

python之使用 Python API 访问交易平台

2024年10月17日8zdz8207

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

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

我可以完成第 4 步并拥有访问 token 。我需要第 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)

如有任何帮助,我们将不胜感激。谢谢。

请您参考如下方法:

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

{ 
    “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 

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

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