2016-06-11 64 views
1

我正在使用python-pipedrive來包裝Pipedrive的API,雖然它不能在python3(我正在使用)上開箱即用,所以我修改了它。我只有Http請求部分有問題。從pipedrive API執行GET請求的Python代碼

這就是教我如何使用httplib2的:https://github.com/jcgregorio/httplib2/wiki/Examples-Python3

基本上,我只是想發送GET請求此: https://api.pipedrive.com/v1/persons/123?api_token=1234abcd1234abcd

這工作:

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'}) 

然而, Pipedrive返回錯誤401'您需要獲得授權才能提出此請求。'如果我這樣做:

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'}) 

實際反應是:

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.'} 

如何正確提供api_token作爲參數(身體)的GET請求?任何人都知道我在做什麼錯了?

回答

0

您需要提供api_token作爲查詢參數。連接這樣的蜇傷

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'}) 
+0

謝謝。但是我認爲body =應該是問號之後的參數?我意識到,我爲域名後面的thing1/thing2 /命名參數令人困惑。 – InfiniteZoom

+0

參數放在'body ='中通常與'POST'請求​​相關。你不應該在'get'請求中使用'body = ...',並且'api_token'不應該放在那裏根據api docs –

+0

陷阱。謝謝! – InfiniteZoom