我正在开发helpshift api,并试图找到一个准确的请求调用,它将返回问题元数据。我试过很多例子,但它总是返回401状态。
但是,我能够让curl命令工作
提供给我的东西是: apikey,url,return是json响应
有效的CURL命令是:
curl -X GET --header 'Accept: application/json' --header 'Authorization: Basic <base64_encoded_version_of_api_key_for_basic_auth>' '<helpshift_url>'我试过的东西如下:
>>> api_key = "ABCDEFGH"
>>> issue = '<helpshift_url>'
>>>
>>> r = requests.get( issue, auth = ( api,"" ))
>>> r.status_code
401
>>>
>>> import base64
>>> api_new = base64.b64encode(api_key.encode("UTF-8"))
>>>
>>> r = requests.get( issue, auth = ( api_new,"" ))
>>> r.status_code
401我要打印的是json响应。
发布于 2019-06-18 09:19:49
requests auth param负责http基本身份验证。根据我在您的代码中看到的,您希望修改头文件,而不是执行auth。
这是通过将头dict headers = {'Authorization': api_new}作为r = requests.get( issue, headers=headers)传递给请求来完成的。
完整的代码是
import base64
import requests
api_key = "ABCDEFGH"
issue = '<helpshift_url>'
api_new = base64.b64encode(api_key.encode("UTF-8"))
headers = {'Authorization': api_new}
r = requests.get( issue, headers=headers)发布于 2019-06-18 09:21:41
您需要使用头部:
>>> import base64
>>> api_new = base64.b64encode(api_key.encode("UTF-8"))
>>>
>>> r = requests.get( issue, header="Authorization: Basic {}'.format(api_new))https://stackoverflow.com/questions/56640581
复制相似问题