如何在pyton中使用api key调用请求

问题描述 投票:0回答:3

我有这样的API: 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:内部服务器错误。

我认为标题有问题。有人可以帮助我吗?

python json api request key
3个回答
0
投票

您可以提供这样的 API 密钥:

from requests.auth import HTTPBasicAuth
import requests

# other code you wrote
auth = HTTPBasicAuth('apikey', 'ILove99Dollars')

response = requests.get(url, headers=headers, auth=auth)

0
投票

我们需要完整的堆栈跟踪来查找错误源。此外,api_key 必须传递给名为 (get_province()) 的函数。

import requests

headers = { 'Accept': 'application/json', 'x-api-key': 'yourapikey'}
search_response = requests.get(url, headers=headers )

-1
投票

从截图来看,应该是

Authorization
而不是
x-api-key

headers = {
        'Content-type': 'application/json',
        'Authorization': api_key
    }

请测试并写出结果是什么

© www.soinside.com 2019 - 2024. All rights reserved.