Android Oauth 2.0

Hafiz Waleed Hussain
U Want To Learn
Published in
3 min readDec 11, 2014

Hi Guys. Now I am starting a new series with name of Social Networking. Basically in this series first I will talk to you about Oauth2.0 module in Android and after that I will try to make Social Network libs like Facebook, Google, Twitter, LinkedIn and many more using our Oauth2.0 Module. So are you ready …

OK. I am using Android Studio 1.0 for this series. In Oauth2.0 I did not tell you about Oauth2.0 instead I talk with you how we use this module in our applications.

Motivation:

In our mostly Android apps, we want to integrate Facebook, Google etc for sign-in or share purpose. For that we add Facebook SDK , Google play service for Google+ and we only want one functionality. Its mean we are adding a long list of methods in our app but we only want one method. Instead many developers face multidex issue due to increase in method limit. Also one more issue, first I need to learn how integrate Facebook then Google then Twitter and a long list. So I feel, I create a one generic module which we use for every Social network. So for that purpose I created a Oauth2.0 Module. So any Social Network who uses Oauth2.0 for authentication, you can use this module for that Social Network.

1. Download EasySocial Module from Github.

2. Create a new project in Android Studio.

3. Import EasySocial Module in our project. Go to File -> Import Module.

4. Now add android dependency. File -> Project Structure.

In above image click the plus button and add as Module dependence.

5. Now create a Simple new activity in our app.

6. Now here we need to take a decision about a Social Network. So I decided, we are using Facebook for this tutorial but you can use Google or LinkedIn or any other which use Oauth2.0. Now create an app on developer.facebook.com and select a Web as platform. Now I have all credential which i need for Facebook authentication. Now final part our activity code.

7. Our Activity.

I think above code is very simple. So going to next part.

Here the above method call when user click a button. Very simple in every Oauth2.0 we need three things.

  1. Login Url
  2. Redirect Url
  3. Access Token Url

The above three Urls we get from our Social Network documentation.

So now when process complete if authentication successful. We will see access token on our screen and if some error occur we will see toast on screen. So now here we have a access token and as we know if we need to get user info, post message or any thing on Facebook we only need to append access token with the url. For example I want to get userInfo, for that I will send Get request on this url.

https://graph.facebook.com/v2.0/me?access_token=xxxxxxxxxxxxx

Screen shoots of App for motivation.

Final thing I am saying this is a generic Oauth2.0 module. For proof, I am commenting some code and setting LinkedIn Url’s and showing you same code work for LinkedIn authentication.

Now after changing in the code. I got result showing below :).

Complete code of testing app also available on Github.

Now I will try to launch next tutorials as early as possible in which we create a Facebook, Google+, LinkedIn and Twitter libs modules for Android in which we will use our Oauth2.0 module.

--

--