在 Python 代码中使用 REST EPA 会出现错误 此代码有什么问题?
Using REST EPA in Python code gives an error What is wrong with this code?
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}
- 错误 1 您正在执行
GET
,文件要求您发送 POST
.
- 错误 2 数据必须采用 URL 编码形式,必须 key-value 对
- 错误3 你必须设置正确的Content-Type header
- 更大的错误不要在public论坛上post你的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
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}
- 错误 1 您正在执行
GET
,文件要求您发送POST
. - 错误 2 数据必须采用 URL 编码形式,必须 key-value 对
- 错误3 你必须设置正确的Content-Type header
- 更大的错误不要在public论坛上post你的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