InfiniteZoom InfiniteZoom - 1 year ago 273
Python Question

Python code to do GET request from pipedrive API

I am using python-pipedrive to wrap Pipedrive's API though it doesn't quite work out of the box on python3 (which I'm using) so I modified it. I'm having trouble with just the Http requests portion.

This is what taught me how to use Httplib2:

Basically, I just want to send a GET request to this:

This works:

from httplib2 import Http
from urllib.parse import urlencode


response, data = http.request(PIPEDRIVE_API_URL, method='GET',
headers={'Content-Type': 'application/x-www-form-urlencoded'})

However, Pipedrive returns an error 401 with 'You need to be authorized to make this request.' if I do this:

parameters = 'persons/123'
api_token = '1234abcd1234abcd'

response, data = http.request(PIPEDRIVE_API_URL + parameters,
method='GET', body=urlencode(api_token),
headers={'Content-Type': 'application/x-www-form-urlencoded'})

The actual response is:

response =
{'server': 'nginx',
'status': '401',
'connection': 'keep-alive',
'set-cookie': 'pipe-session=7b6ddadbc67abdadb6a67dbadcb; path=/;; secure; httponly',
'date': 'Sat, 11 Jun 2016 06:50:13 GMT',
'transfer-encoding': 'chunked',
'x-frame-options': 'SAMEORIGIN',
'content-type': 'application/json, charset=UTF-8',
'x-xss-protection': '1; mode=block'}

data =
{'success': False,
'error': 'You need to be authorized to make this request.'}

How do I properly provide the api_token as a parameter (body) to the GET request? Anyone know what I'm doing wrong?

Answer Source

You need to provide the api_token as a query parameter. Concatenate the stings like this

route = 'persons/123'
api_token = '1234abcd1234abcd'

response, data = http.request(PIPEDRIVE_API_URL + route + '?api_token=' + api_token, 
    headers={'Content-Type': 'application/x-www-form-urlencoded'})
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download