Django rest authentication

I developed the API using the django rest framework, and used token based authentication. Now I am trying to use it from a separate project. How can I log in using userid and password and get the token in response and use the token in the header in all subsequent URLs.

From Shell I checked the token for one of the users and tested the api from the terminal, and it works like

http://127.0.0.1:8000/corporate/company/ -H 'Authorization: Token 9f4702dfddbf89e0346b2ffd10fd69173c178273' 

But how to use this token in http calls?

I included rest_framework.authtoken in the installed application and included the url in urls.py as:

 url(r'^api-token-auth/', 'rest_framework.authtoken.views.obtain_auth_token') 

Now I'm trying to access it from another project, where did I do one of the login forms? Now the question is where to place the form and which fields should be in the form. If I placed the form, the token will be returned in response, then how can I parse and use in the headers for the next calls?

I looked through the manual and the API manual, but it did not help. About how to access the api in my project, while Api is ready, and the login works through the browser api url.

+4
source share
1 answer

Working with the Django framework supports many auth options. You can use Basic Auth if you want. If you want to use the token, you will need to set the http header with the correct token for your user.

From the docs you should set it as (replace 99 ... with yours)

 Authorization: Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b 
+3
source

Source: https://habr.com/ru/post/1489271/


All Articles