OAuth with Facebook

Most of the applications over web these days need to get connected with each other. With the surge of Social Aware applications people started integrating all of their social application in one location.  Following is my attempt to solve the "Complex" but easy puzzle for novice developers

To start with i’ll demonstrate how to Login with Facebook and details of facebook user into your application

Lemme first explain what OAuth means in Brief.  Below is the diagram which explains

First we call Service provider with  ClientID  / CallBack URL  and Some additional Parameters based on Service provider you use

Then if we pass the right parameters then Service provider will give us a call back to given callback URL with Authorization Token and some parameter that states time to live

Now we can use this Authorization token in our subsequent requests to Service Provider Provided services

Here is example of facebook

Step 1

Goto Facebook developer Page

Step 2

Register your App. You will get you app key/secret etc

Step 3

Now goto this sample URL to get your token

Test with this URL

https://graph.facebook.com/oauth/authorize?type=user_agent&client_id=168452316544703&redirect_uri=http%3A%2F%2Fnorthalley.com%2FTestWeb%2F/GetIPServlet&scope=user_photos,email,user_birthday,user_online_presence

Replace Corresponding Tokens for your Application

https://graph.facebook.com/oauth/authorize?type=user_agent&client_id=CLIENT_ID&redirect_uri=REDIRECT_URL&scope=user_photos,email,user_birthday,user_online_presence

Step 4

Once you get Call back get the access_token from the request and store it

Example

—————–

http://northalley.com/TestWeb/GetIPServlet#access_token=168452316544703%7C2.WKMyHmlbzscco7HjrkJUMw__.3600.1304560800.1-100000131478113%7C9IpaoHOvCIfS4bqRZSqbu5-Rp8E&expires_in=5233

Step5

——–

Get user Data using the above token.

https://graph.facebook.com/me?access_token=168452316544703|2.WKMyHmlbzscco7HjrkJUMw__.3600.1304560800.1-100000131478113|9IpaoHOvCIfS4bqRZSqbu5-Rp8E

This gets user data in following format

{
   "id": "100000131478113",
   "name": "Ashwin Kumar",
   "first_name": "Ashwin",
   "last_name": "Kumar",
   "link": "http://www.facebook.com/profile.php?id=100000131478113",
   "birthday": "06/24/1984",
   "hometown": {
      "id": "103095893064172",
      "name": "Rajahmundry"
   },
   "location": {
      "id": "103129393060364",
      "name": "Allentown, Pennsylvania"
   },
   "work": [
      {
         "employer": {
            "id": "111926488822704",
            "name": "Freelancer"
         },
         "position": {
            "id": "130875350283931",
            "name": "CEO & Founder"
         },
         "start_date": "0000-04",
         "end_date": "0000-00"
      },
..............
...........
.............

Njoy integrating facebook to your application. Subsequent series will Cover Google/Twitter/LinkedIn/Yahoo Integration. Also writing a java library for this.. Will share that in opensource once done

Njoy playing with social apps….

6 thoughts on “OAuth with Facebook

  1. Thanks for a marvelous posting! I actually enjoyed reading
    it, you could be a great author.I will make certain to bookmark your blog and will eventually
    come back sometime soon. I want to encourage you to continue your great writing, have a nice day!

  2. Its lioke you read my mind! You seem to now so mucfh about this,
    like you wrte the book inn iit or something.
    I think that yyou could do wth a few pics to drive the message home a
    bit, but other than that, this is magnificent blog. An excellent read.
    I will definitely be back.

Leave a comment