Difan Zhao
Difan Zhao

Reputation: 389

How to send basic authorization header with Python3.6.9 urllib3

I am trying to send an API call to get the time from the Questrade platform. Here is the sample request from their guide

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

I am able to get it working with the request module

headers = {'Authorization': f'{token_type} {access_token}'}
print(headers) -> {'Authorization': 'Bearer -xSoUNCLYCrFjxxxxx_wAQVpi4olWrQs0'}
qt_time_obj = requests.get(api_server + 'v1/time', headers=headers)
qt_time = qt_time_obj.json()['time']
print(qt_time) -> 2020-10-13T17:06:32.388000-04:00

Now I am trying to get urllib3 to work but without luck

headers = {'Authorization': f'{token_type} {access_token}'}
url = api_server + 'v1/time'
http = urllib3.PoolManager()
qt_time_obj = http.urlopen('GET', url, headers)

print(qt_time_obj.status) -> 401
print(qt_time_obj.data) -> b'{"code":1014,"message":"Missing authorization header"}'

I also tried with the make_headers method but it gives me the same error.

headers = urllib3.make_headers(basic_auth="Authorization: Bearer AdKt3YUl46_tGnZp7cRgTu4W2vtfBME50")

Could you point where I did wrong? Thank you!

Upvotes: 0

Views: 2411

Answers (1)

Difan Zhao
Difan Zhao

Reputation: 389

So after some trying, I found that I need to use http.request instead of the http.open. I also need to do "headers=headers" instead of just the "headers" in the method.

qt_time_obj = http.request('GET', url, headers=headers)

Upvotes: 2

Related Questions