'How to create key to get a request to Django API?
I am creating a database with Django, and I am accesing it with Excel and Apps Script. I can't log in into my superuser account (the only account I'll be having). Therefore, I want to know if there is a way to set a key, or a password, passed through an HTTP header, that I can use to access my API.
Currently I only have one API get method, but I'll have more create, update, etc., and I want to be able to use the same key for these as well. It is a really simple Django application deployed in heroku, so I am not using django-REST-framework, nor graphene-django. So if the solution can come without installing anything else, it would be nice. However, if this is not possible, it is better if the solution comes through graphene-django, since I have used it before, and I am more familiar with it.
It is important to notice that both the excel file and the apps script are private, since I am using django as a database manager for a small, personal project.
Thank you very much in advance!
Solution 1:[1]
Have you tried using djangorestframework-api-key?
The process is as simple as
$ pip install djangorestframework-api-key
# settings.py
INSTALLED_APPS = [
# ...
"rest_framework",
"rest_framework_api_key",
]
...
REST_FRAMEWORK = {
"DEFAULT_PERMISSION_CLASSES": [
"rest_framework_api_key.permissions.HasAPIKey",
]
}
$ python manage.py migrate
and then your curl should look like this
$ curl -H "Authorization: Api-Key <API_KEY from admin page>" http://www.example.com
Solution 2:[2]
REST implementation of Django authentication system. DJOSER
Also, you need a MOD HEADER which is an Extension in Chrome Add it from here
Once your Django project is up and running go to
localhost:8000/auth/jwt/create/ for creating access token by submitting username and password (ie: POST method)
With this access token you need to set it in MOD HEADER in Request Header and you are good to go.
it's a JSON web token that's why you need to prefix it with JWT and then access token
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | iamjazzar |
| Solution 2 | Azhar Uddin Sheikh |

