我正在研究 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_code401>>> >>> import base64>>> api_new = base64.b64encode(api_key.encode("UTF-8"))>>> >>> r = requests.get( issue, auth = ( api_new,"" ))>>> r.status_code401我想要得到的是打印的 json 響應
2 回答

侃侃爾雅
TA貢獻1801條經驗 獲得超16個贊
您需要使用標題:
>>> import base64
>>> api_new = base64.b64encode(api_key.encode("UTF-8"))
>>>
>>> r = requests.get( issue, header="Authorization: Basic {}'.format(api_new))

蝴蝶刀刀
TA貢獻1801條經驗 獲得超8個贊
requests auth param 負責 http 基本認證。根據我在您的代碼中看到的內容,您不想執行身份驗證,而是要修改標頭。
這是通過將 headers dict 傳遞headers = {'Authorization': api_new}給 requests as 來完成的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)
添加回答
舉報
0/150
提交
取消