REST API 檔案
---------------------------------------------------------------
curl --location --request POST 'https://zaya.io/api/v1/links' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Authorization: Bearer {api_key}' \
--data-urlencode 'url={url}'
-----------------------------------------------------------------
我的代碼
import requests
api="https://zaya.io/api/v1/links"
API_KEY = "################################################"
data = "https://en.wikipedia.org/wiki/Python_(programming_language)"
headers = {'Authorization': 'Bearer ' API_KEY}
r = requests.get(api, data=data, headers=headers)
print(response.text)
我面臨的錯誤:
{"message":"You are not logged in.","status":403}
uj5u.com熱心網友回復:
- 錯誤 1??您正在執行
GET檔案要求您發送POST. - 錯誤 2資料必須是 URL 編碼的形式,必須是鍵值對
- 錯誤 3您必須設定正確的 Content-Type 標頭
- 更大的錯誤不要在公共論壇上發布您的 API 密鑰。
下面的代碼可以正常作業,并進行上述更正。
import requests
api = "https://zaya.io/api/v1/links"
# Never reveal your API key
API_KEY = "##########################################################"
# Data is URL Form encoded, so it must be key-value pair
data = {"url": "https://en.wikipedia.org/wiki/Python_(programming_language)"}
# Add proper headers
headers = {'Authorization': 'Bearer ' API_KEY, 'Content-Type': 'application/x-www-form-urlencoded'}
# Most important. CURL says POST, you did GET
r = requests.post(api, data=data, headers=headers)
print(r.text)
# Gives proper response
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/460463.html
標籤:python-3.x api 休息
