Tuesday, May 9, 2017

Oh-auth

Lets Create A Facebook App



What is OAuth??

OAuth (Open Authorization) is an open standard for token-based authentication and authorization on the Internet. OAuth, which is pronounced "oh-auth," allows an end user's account information to be used by third-party services, such as Facebook, without exposing the user's password.




What is this token Facebook provides? actually Facebook provides two types of tokens. Access token and refresh token. Access token can be used several times before it gets expired. Once it is expired refresh token is sent to Facebook server in order to receive another access token along with a new refresh token.User can use this access token to get information from Facebook.

Before things getting started you need to have a developer's account in facebook.

https://developers.facebook.com/


Then you should login with your facebook credentials which will create a developers account related to your normal facebook account.

You can give any name to your App name proceed with "Get Started".


And you should provide a valid redirect URL. 
If you have a hosted website you can provide the path for that. 

For the moment I will set the redirection URL to my localhost. Which the server is glassfish.



Then you might need to add the domain of the Settings basic tab.



Obtain Authorization code from Facebook

For this we have to prepare the URL. This URL contains for elements.When we put these elements together all should be encoded using a URL encoding method. Parameter name, value and encoded value is given below. 

https://www.facebook.com/dialog/oauth?response_type=code&client_id=159330037933855&redirect_uri=http%3A%2F%2Flocalhost%2Fteam%2F&scope=public_profile%20user_posts%20user_friends%20user_photos

Enter that URL in the URL bar of your browser and hit enter. Now you will see something like this. This is called as user consent page. In there you can see "Edit this" button. If you click on that you can manage the accessing resources.



Since you are the owner of this App you don't have to worry about privacy. Click on continue.

This page will appear. 





This page appear because for real you don't have a project which supports http://localhost/team/.
But check the URL. You can see authorization code is sent to you from Facebook. (highlighted)


http://localhost/facebookapp/?code=
AQBwPkipam2oyyypNH_AyGj0zx5-neWyNLcQEF4D1EvrU30aNBwuBGIvFptwSpQ3O9zAHz4sYw26gBSbTpFgjnuOwgCGY7hwhTH2qskEAxzml_liVN9XbUD6jbDW-8Vs0OQpSrzf_CjNpLZOTRjCfellS0Wu2ctYUPXKw1CHzwzBnHLGQMbhV1RqmO-gDgsz0z_9n9E0nfTbxWF5wIINGKHNYTG3r4mwgBN1EIjYdmvyepGxoWe_roMnRC0G7qllHncieEB4_DOXXWJBaCYIP6yii7DPQ1AMRJzlFZcnKYkqTWrEKP276OrakXry96YmnYQbpEBNxnXbygr3dwCvzMcr#_=_

Obtain access token

To obtain access token we have to have four parameters.

1. grant_type Authorization_code

2. client_id 159330037933855

3. redirect_uri http://localhost/facebookapp/

4. code AQAka5fVz7A0v78CuKDhUItSO-GnnQaGr-ZtcqlQS8CuSDNlyzYL2Qf-yGmpRCm6Kbhlh2J_-jZiYPnpNkTapKIQGl2RQxamgUa1rlARnvkd2xWWbwwcJVoYJNrvF0qqHt0M1rG0WCk2I3DPdSUqfmLndGpLoL9xspVxsF4nOYvRa1VqRE7qDpvnDC5MClTHUIg24zbhyl56DIUkOZDmgfLyoloCCDxJkAYwZvgWblmFQc3T5p4AZYpgMFOyZM4JYdyRotLNOSMrL1zFW_bjWiAJ0HtmQkN4NcVyLvafSTj3Nq0z4oHHxVPxpSEfmPhH639gT20M3M-jC_DckMmvU#_=_


In the HTTP Headers, we need to add the Authorization header with the App credentials. 

App ID -  159330037933855
App secret - 12098ed8b69fbd3b81355997b099566c

AppID:App_secret
159330037933855:12098ed8b69fbd3b81355997b099566c

Now we have to encode this whole value using a base64 encoder.

MTM2MzE4MDM0NzEwODkwNzpjMTJmYjk0MGNhOGU2N2Q0M2Q
0NDdmMzY0ODYxMjE4Yg==

To get the access token we have to specify the token endpoint. In this case it is this url
https://graph.facebook.com/oauth/access_token .  
  
Install RESTClient in your browser.

Give those values and obtain access token.




Retrieve resources using access token

Method - GET
URL - https://graph.facebook.com/v2.9/me?fields=id
Authorization: Bearer <access token value>

 This will give user's ID in JSON object format.
 Using this ID you can get any information you want.

ex- you can uploaded posts.


https://developers.facebook.com/docs/reference/php 



Okay, That's it.hope you guys got an idea about the Facebook apps and OAuth.
Now you can create your own app.







1 comment:

Get Unlimited Free Trials Using a "Real" Fake Credit Card Number

When I see the words "free trial," I know I'm probably going to have to whip out my credit card and enter in the number to &qu...