Retrofit Tutorial — Token Authentication

preview_player
Показать описание
In this video you'll learn about token authentication and how to implement it with Retrofit.

Tip: turn on subtitles to deal with my accent. A shortened transcript is also available below.

Find the tutorial for an easy read here:

Watch 20+ Retrofit videos in our playlist here:

----------------------------------------

Our book on Retrofit is also available on leanpub:

----------------------------------------

Checkout 320+ technical in-depth tutorials:

Subscribe for two new videos every week:

----------------------------------------

Follow us on social media to get updates on new content:

----------------------------------------

Shortened transcript:

In the last video we have looked at basic authentication so a standardized way on how you can identify your user to your server. In this video we will look at the less standardized version of it, which is total authentication. Instead of encoding the user name and password on every request and sending it with it, we are getting a special token. Most APIs create the token when you log in and return it to you and all subsequent calls are going to be made with that token. Basically all protected endpoints expect that token as the authorization header.

In this video we will look at how you can implement that on an android app with Retrofit. Now it's important that you do this on an HTTPS environment. Otherwise this can be read by anyone and you shouldn't send passwords in clear text. In this case the response is a user ID, the email and also that secret token. The token is important! You need to save that because you will need it for all following requests. Here we have a second endpoint which is getSecretInfo and if you're sending this without any token you will get a 401 back. Basically the server is telling us "hey I won't give you that information unless I get a valid token from you" Most token APIs expect the token in the authorization header. Use the authorization as the header key and the value will just be that special token we just got. If you send it now you'll get an interesting string back. Once again if we change this token to something else invalid. So we send a token but it's not correct, we will get that error message back.

Let's implement all of that: the login and then the getSecretInformation() on the Android app. Let's describe the Retrofit interface. The first interface we need to describe is the login. As you have seen in Postman this is a POST request and the URL is simply /login. We expect a user object back, so this is going to be user and let's call this just simply "login()". The request body will be the login class I have just showed you. The second part is the secretInfo(). This is simply /secretInfo. As you have seen in Postman, it's a GET request since we are just requesting data. Since it's just a string, we won't to any transformation into a Java class and this will be getSecret(). In this request we need to send the token with. We're going to add an additional parameter, which is going to be a @Header. This will be a string and it will be the auth token you want to send. Now we have to tell the Retrofit that this token is going to be the authorization header.

Okay so first step is you're going to log in with the login information. That login gives us the user object back, where we have the token. Then we will use that token to the request the secret and the secret is just a string, so we won't to any parsing. Let's jump into the activity and implement all of that. That's it! In this quick and simple example you have seen how you can use the token authentication to log in the user, get the token and use a token for further requests. Obviously when the API gets more complex with more endpoints this is still not ideal. You should save the token in the more central place and, ideally, you also handle the authorization header in a more central place. One option is the ServiceGenerator which we will show you in a separate video in the future.
Рекомендации по теме
Комментарии
Автор

Hi, does Call<User> handle nullable response?

krystian
Автор

thank you very much for the clear and useful information!

cjrd
Автор

Thank You Very Much !! :) that Kicked started my project :)

quenar
Автор

Thanks for the tutorial! What's the simplest way to handle when a token is expired every 10 minutes? Will you be covering this in a future tutorial? Keep up the great work! :)

hawkybae
Автор

Do you have a preference for storing auth tokens? Shared preferences or db?

ericdgood
Автор

@POST -> I'm getting annotations are not allowed here

shwappler
Автор

how to do it when the login and password are entered by the user

liftoffbeats
Автор

Thank you for sharing such a good tutorial. It was really helpful.

JuniorCRodriguezV
Автор

Thank U for this tutorial. but i have some of problem. in postman, u have sent "content-Type " is "raw(Text)". but my server has to send "content-Type: raw(json)". and my application is not work. I don't know how to do it. Please help me. Sorry, my english is not good

hmdarknight
Автор

thank you very much for this video, it helping me.

aminesquall
Автор

I have made api file (.js) for my app but do not know how to create token. pls help!!

footballforfun
Автор

Can you please zoom in the screen.. The coding is not visible

yugandharyugandhar
Автор

Using conceal is good enough to store token?

monoomankar
Автор

how would you deal with token expiration
do you store the token on the phone and if the token is there then the user is logged in or do you check regularly with the server is the token is still valid?

cunningham.s_law
Автор

Where do i find someone to help me with my app lol I cant for the life of me get it working.

HartleyTopTips
Автор

Hi, where can I find the code of the API you are using?

angelamilosheska
Автор

Thank you for sharing such a good tutorial. I would like to request for upload video to send request with SSL certificate using retrofit.

sohamnavadiya
Автор

how to use with JWT? I need example code

AminNajafi
Автор

Sorry sir I m just a beginner in android I did this with local storage in angular cli but I want to store that token in cache in android app can you please also show that????

TheCodebookInc
Автор

hi if i am using Bearer Auth then that should i do in my UserClient.java

neerajupa