Okay.
In this video we will be talking about token and I will show you how we can generate a token, how we
can use a token.
And later in the tutorial I will show you how we can use Token to authenticate some of our API.
So basically, what is a token?
A token is a random string of characters that will be auto generated by our Django, and this secret
string of characters will be used to authenticate the users.
So basically what we can do is we can tell part of our API or the whole API, we can block it unless
we will, the user will provide a token.
So what we want to do is give an option to the user that he will log in with a username and password
and will return for for the correct credentials will return a token.
And with that token you will need to send it every time.
He wants to have a piece of data from our application, you will need to send it along if the token
is wrong or expired or there is no token, we are not going to return anything.
So how can we actually do this?
This is built in Django rest framework functionality.
So what we need to do is we need to go go to our application, which is first and then go to settings.
And here we have rest framework.
So we need to also add rest framework and then dot out token like that I will put command end.
So we need to add another application to install apps.
I will save it here and what we need to do once we add something in the apps, you can see a message.
Even here you have to unapplied migrations.
So what we need to do is we go Python three, manage pi and then migrate.
So that will apply to migrations from authentication token and apps and that's available for us to use
now.
So our server is running, we'll click on this and then I will go to admin.
We are inside our admin.
How login with my credentials the same as I've done before.
And here we have another option, another section which is authentication token.
And here we have tokens.
So we click here, no tokens there.
Before we will add a token, I will show you that on the home we have inside users, we have one user.
This user has been created before.
We had that authentication token.
Enabled.
So there is no token associated with this user.
So let's fix that.
I will go to home and then to token I can click add here or from the tokens, I will add it from here
and then I can pick for what user I would like to generate.
I will generate one for Christian.
If you have that enable the token will be generated automatically for new users.
But as I said, the Christian has been created before we had that authentication token enable.
So there is it's not available here.
I will just save.
And I can see this string here that's a secret token for that specific user.
So every time you generate a new token or a new user, this random string of character characters will
be generated automatically for you.
So this is done already.
So we have our token generated for our user.
So what can we actually do about it?
At the moment we can't do anything about it, so let's enable that option to get that token when the
user will provide his credentials.
So what we can do, go back to our application and we'll go back to URLs.
So here we'll create another URL for authentication.
The user this time, maybe I don't want to do it in the demo, I can do it in this URL or I can do in
the URL for my application for my project.
So on top of that I can do pad.
And then I will do.
But I can name it whatever I like, but I will do it out so it's short and nice and then whatever you
will do your.
URL slash out.
Then we'll trigger the view for authentication.
We need to import it first.
So from.
Rest framework.
Outspoken.
Views import?
Obtain authentication token and this obtain authentication token.
I can use it here in URLs.
So basically what I am doing is I have that view available from that library authentication token and
then I apply it to a URL out.
So I'll save it now.
And will run the server.
And we'll open Postman.
And that's our postman.
So we can go to our authentication, that's our URL we have created and then I can do send.
So first thing, Methodget is not allowed.
Why this is happening Because the authentication requires my credentials, which is username and password
and methodget returns something.
So if you want to send something to our server then we need to use a post.
So I will switch to post and let's try the same method again.
It still doesn't return anything to us because we need to provide a username and password so we can
do that.
Going to body.
And then what I can do is I can do username here and then password here.
So username.
As you remember we've created super user with user.
Christiane and password.
Christiane.
So username and password.
This time when I use the post, I will send username and password.
I will send it now.
And you can see here in return, I got this string of characters and you can see B5 five eight.
Is the same B5 eight as you have it here.
So this token.
Is returned from our out URL when I provide username password.
So basically that's our login functionality built in.
So we provide username and password, send it to Django.
Django, authenticate that user and check it.
Okay.
We have user with name username, Christian and password Christian and then in return we will give me
that token.
So that token at the moment is useless because we don't use it anywhere.
And in the next video I will show show you how we can actually protect our API, our resources on our
database only allow users with that token to see our databases record.