我有这样的API:
我想在python中调用这个api,这是我的代码:
def get_province():
headers = {
'Content-type': 'application/json',
'x-api-key': api_key
}
response = requests.get(url, headers=headers)
return response.json()
但是,我有
错误 500:内部服务器错误。
我认为标题有问题。有人可以帮助我吗?
您可以提供这样的 API 密钥:
from requests.auth import HTTPBasicAuth
import requests
# other code you wrote
auth = HTTPBasicAuth('apikey', 'ILove99Dollars')
response = requests.get(url, headers=headers, auth=auth)
我们需要完整的堆栈跟踪来查找错误源。此外,api_key 必须传递给名为 (get_province()) 的函数。
import requests
headers = { 'Accept': 'application/json', 'x-api-key': 'yourapikey'}
search_response = requests.get(url, headers=headers )
从截图来看,应该是
Authorization
而不是x-api-key
即
headers = {
'Content-type': 'application/json',
'Authorization': api_key
}
请测试并写出结果是什么