Retrofit Tutorial — Basic Authentication
In this video you'll learn about basic 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: ►https://futurestud.io/tutorials/android-basic-authentication-with-retrofit Watch 20+ Retrofit videos in our playlist here: ►https://www.youtube.com/playlist?list=PLpUMhvC6l7APq7y_FFfK-GEHvcUKqo6SC ---------------------------------------- Our book on Retrofit is also available on leanpub: ►https://leanpub.com/retrofit-love-working-with-apis-on-android ---------------------------------------- Future Studio is helping 5,000+ users daily to solve Android and Node.js problems with 320+ written tutorials and videos. We’re on a mission to provide new in-depth content every week. Checkout 320+ technical in-depth tutorials: ►https://futurestud.io Subscribe for two new videos every week: ►https://www.youtube.com/c/FutureStudio?sub_confirmation=1 ---------------------------------------- Follow us on social media to get updates on new content: ►https://twitter.com/futurestud_io ►https://www.facebook.com/FutureStudioUniversity ►https://plus.google.com/+FutureStudioUniversity ---------------------------------------- Shortened Transcript: This video will be about basic authentication. Authentication is a process where you're identifying yourself as a "special someone". If your app has login process, in that moment where the user enters username and password he identifies himself as one particular user of your service. Basic authentication is the standard where you send the user name and the password as the base64 encoded request header to the server. The server then can take the username and password and make sure it's in the database and it's correct and then allow the action you want to do. If you want to start implementing this on Android, we need to do several things. First of all we need to add the authorization header. Then you need to create this encoded part back here and then finally we just send the request with Retrofit. I've prepared an empty login app. As always with Retrofit, we need to start by describing the interface. We have just seen the method is actually a GET method, under the route /basic. Let's call this simply getUser() and we expect a user object back. What we still need to do is add the authorization header. You can add a header by simply adding another parameter here, which is going to be a string, and it will be the auth header. And the value of the annotation will be authorization. We have the interface, so now we need to talk to that interface. Let me jump into the login activity. We will use the UserClient interface and use the getUser() method to do our call. The header is actually the value of that key. So we will start this basic and we have that empty space and back here we need to add the encoded username/ password. You can do that by using the base64 class and here you already have the encode method. We can do encodeToString() and now we need to give it a byte array. That byte array is the string combination. What we're going to do now is we're going to add the username and password and we separate those two by a simple colon [:]. What's going to happen here? We use our user name, we use the password and we combine that with the colon. Then we encode it with the base64 class and that is readable to the server. The server can decode that and check if it's a real user. Of course, we still need to get our Call object down here since we are already in a background thread with the async task, we can simply use the execute() way, so the synchronous way of doing that network request. This will be the server response object. As you know if we do execute() we need to catch network failures, which is going to be the IOException and finally we need to check if the response was successful and in that case we will return true. The final thing to do is instead of hard-coding the login which doesn't make any sense, we will tie it up to the UI. And then we will start this thing. Let's review what you have seen in this video. In this video you have seen how you can authenticate your user using the basic authentication to a server.[No pun intended] Basically what you're doing is you're encoding the username and password into a base64 string and sending that to the server. That encoded string needs to be an authorization request header. You need to make sure that that request header has the exact format of authorization as the header key and let the value starts with"Basic " and then that encoded string. Always make sure you have the correct format. One disadvantage of the way I just showed you is that you would have to repeat this over and over again. Better is if you have a central place where you always generate that authorization header and that can be done by using a ServiceGenerator.
Download
1 formatsVideo Formats
Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.