InfiniteZoom InfiniteZoom - 5 months ago 101
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: https://github.com/jcgregorio/httplib2/wiki/Examples-Python3

Basically, I just want to send a GET request to this:
https://api.pipedrive.com/v1/persons/123?api_token=1234abcd1234abcd

This works:

from httplib2 import Http
from urllib.parse import urlencode

PIPEDRIVE_API_URL = "https://api.pipedrive.com/v1/persons/123?api_token=1234abcd1234abcd"

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:

PIPEDRIVE_API_URL = "https://api.pipedrive.com/v1/"
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=/; domain=.pipedrive.com; 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

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

PIPEDRIVE_API_URL = "https://api.pipedrive.com/v1/"
route = 'persons/123'
api_token = '1234abcd1234abcd'

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