【问题标题】:authorize api keys in django在 django 中授权 api 密钥
【发布时间】:2020-08-17 03:19:42
【问题描述】:

如何验证 django 中的 api 密钥?我正在使用 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': "2b01c1685cmsh18f385b26cfee59p164749jsn3684ad96eaca"
    }
    r = requests.get(url)
    json_data = json.loads(r.text)
    print(json_data)
    return render(request,'portofolio/thankyou.html',headers)

打印返回“{'message': 'Missing RapidAPI application key. Go to https://docs.rapidapi.com/docs/keys to learn how to get your API application key.'}” 我有钥匙,如何授权?并在我的 django 模板中使用它!在此先感谢:)

【问题讨论】:

    标签: python html django api


    【解决方案1】:

    您需要将标头发送到 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': "2b01c1685cmsh18f385b26cfee59p164749jsn3684ad96eaca"
        }
        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')
    

    【讨论】:

    • 那么我要在模板中使用 wt 吗?
    • 好吧,你能更好地描述你想要执行的流程吗?
    • 在我的模板中显示这个的输出? nvm 我明白了,谢谢你的想法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多