Search code examples
pythonhtmldjangoapi

authorize api keys in django


How to verify the api keys in django? im using rapidapi and they have this key and how do i authorize it with the 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)

The print returns "{'message': 'Missing RapidAPI application key. Go to https://docs.rapidapi.com/docs/keys to learn how to get your API application key.'}" I have a key and how do I authorize it? and use it in my django template! Thanks in advance :)


Solution

  • you need to send headers to 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')