2

I'm new to getting data using API and Python. I want to pull data from my trading platform. They've provided the following instructions:

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

I'm ok up to step 4 and have an access token. I need help with step 5. How do I translate this request:

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

into Python code? I've tried

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

I tried that after reading this: python request with authentication (access_token)

Any help would be appreciated. Thanks.

6
  • 1
    headers = {'Authorization': 'Bearer C3lTUKuNQrAAmSD/TPjuV/HI7aNrAwDp'} Commented Oct 29, 2016 at 19:18
  • doesn't seem to work. Getting <Response [401]> Commented Oct 29, 2016 at 19:35
  • you should substitute in your real access token for the one after Bearer in the example above. But that is the format of the header that's expected. A 401 error is Unauthorized, which means the token is invalid. Commented Oct 29, 2016 at 19:39
  • I did substitute my real access token. I'll check with the trading platform why this is happening... Commented Oct 29, 2016 at 19:44
  • step 4 is the "login" functionality, have you exchanged the token you got from the user frontend for the access_token object from the response? has it expired in the interim, possibly? Commented Oct 29, 2016 at 19:47

2 Answers 2

3

As you point out, after step 4 you should have received an access token as follows:

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

To make subsequent API calls, you will need to construct your URI as follows:

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

Next, construct your headers as follows:

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

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

Finally, make your requests call as follows

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

Hope this helps!

Note: You can find a Questrade API Python wrapper on GitHub which handles all of the above for you. https://github.com/pcinat/QuestradeAPI_PythonWrapper

Sign up to request clarification or add additional context in comments.

Comments

1

Improving a bit on Peter's reply (Thank you Peter!) start by using the token you got from the QT website to obtain an access_token and get an api_server assigned to handle your requests.

# 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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.