在django中授权API密钥

qni6mghb  于 2023-05-23  发布在  Go
关注(0)|答案(1)|浏览(263)

如何验证django中的API密钥?im使用rapidapi和他们有这个密钥,我如何授权它与url
views.py

def thanks(request):
    url = "https://webknox-trivia-knowledge-facts-v1.p.rapidapi.com/trivia/random"
    headers = {
        'x-rapidapi-host': "webknox-trivia-knowledge-facts-v1.p.rapidapi.com",
        'x-rapidapi-key': "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    }
    r = requests.get(url)
    json_data = json.loads(r.text)
    print(json_data)
    return render(request,'portofolio/thankyou.html',headers)

打印返回“'message':'缺少RapidAPI应用程序密钥。转到https://docs.rapidapi.com/docs/keys了解如何获取API应用程序密钥。'}”我有密钥,如何授权?并在我的Django模板中使用它!提前感谢:)

8oomwypt

8oomwypt1#

您需要将头发送到rapidapi:

def thanks(request):
    url = "https://webknox-trivia-knowledge-facts-v1.p.rapidapi.com/trivia/random"
    headers = {
        'x-rapidapi-host': "webknox-trivia-knowledge-facts-v1.p.rapidapi.com",
        'x-rapidapi-key': "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    }
    r = requests.get(url, headers=headers) # here you need to send headers
    json_data = json.loads(r.text)
    print(json_data)
    return render(request,'portofolio/thankyou.html')

相关问题