Saturday, January 15, 2011

How to use facebook application into your site.

I assume you know already how to create a facebook application from my previous post, coz on this post, I will tell you how to use it in your own website.

We will be using the OAuth process which is the legal way in posting facebook status.

Just follow the simple steps below and we can have our simple facebook app working in our own website.

1. first, you have to get your facebook app ID from - http://www.facebook.com/developers/apps.php

2. register the URL of your site by clicking the "Edit Settings" link. go to Web Site Settings by clicking the "Web Site" tab, then type in the URL of your site. just for testing, we can always use our local domain, coz facebook allows it. ex. http://localhost/facebook/

3. after registering our site URL, we can now create a simple app that will request to allow our facebook application to access profile and submit status.

4. just for testing purposes, I am allowing everyone to use my own created facebook application "Hotshots Point Of View". this application is configured to use your own local domain. please see details below.

app ID -  182635521758593
app Secret - 495625ad928ea277548d0f423f420ef0
site URL - http://localhost/facebook/

5. if you will use my facebook app, you must create a "facebook" directory under your localhost domain. copy the code below and save it as index.php. on the URL below, app ID will be your client_id and the site URL will be your redirect_uri.

<html>
<head>
</head>
<body>
<a href="https://graph.facebook.com/oauth/authorize?client_id=182635521758593&redirect_uri=http://localhost/facebook/">authorize app</a>
</body>
</html>

6. this is just a simple interface with a link labeled as "authorize app". go to your created app - http://localhost/facebook/ then click the "authorize app" link. you should be redirected to facebook page requesting permission to allow "Hotshots Point Of View" to access your basic information, if you are not currently logged-in, you will be prompted to log-in.

7. If the user authorizes your application, facebook will redirect the user back to the redirect URI we specified with a verification string in the parameter CODE - http://localhost/facebook/?code=.... the CODE parameter can be exchanged for an OAuth access token for us to be able to access users profile.

8. to capture the value of the CODE parameter, we will modify our index.php as below. use the CODE to access token URL. pass the same client_id and redirect_uri as in the previous step together with the CODE and App Secret as stated above. this will now be the content of our index.php.

<?
$code = $_GET['code'];
?>

<html>
<head>
</head>
<body>
<? if (!$code) : ?>
<a href="https://graph.facebook.com/oauth/authorize?client_id=182635521758593&redirect_uri=http://localhost/facebook/">authorize app</a>
<? else : ?>
<a href="https://graph.facebook.com/oauth/access_token?client_id=182635521758593&redirect_uri=http://localhost/facebook/&client_secret=495625ad928ea277548d0f423f420ef0&code=<?=$code?>">access token</a>
<? endif; ?>
</body>
</html>

9. if CODE parameter doesn't have value, the link will be labeled as "authorize app" and if it has, the link will be labeled as "access token". try accessing your app again - http://localhost/facebook/

10. after clicking the "access token", it will goes back again to your site and echo the access_token which you will be use to access users profile.

11. save the access_token returned by facebook then use it to access profile. please see simple code below in PHP to get the users profile.

<?
$token = '<access token>';

// get user info
$infourl = "https://graph.facebook.com/me?access_token=$token";

$url_handler = fopen("$infourl", 'r');
$url_contents = stream_get_contents($url_handler);
fclose($url_handler);

$return = json_decode($url_contents);

$userid = $return->id;
$fname = $return->first_name;
$mname = $return->middle_name;
$lname = $return->last_name;

echo "$fname $mnane $lname\n";

?>

12. now, that you were able to get the users profile, you can have that integrated within your site.

Hope you were able to follow the steps I made. submission of facebook status will NOT be able work on this post coz we have to specify a SCOPE to authorize our application to publish stream but don't you worry coz that will be next. Enjoy!! yeah men!!

No comments:

Post a Comment

Leadership 101


  • Leadership demands sacrifices for the near-term to receive lasting benefits. the longer we wait to make sacrifices, the harder they become. Successful people make important decisions early in their life, then manage those decisions the rest of their lives.
  • Growth does not happen by chance. If you want to be sure to grow, you need a plan something strategic, specific, and scheduled. it's a discipline that would need incredible determination from us.
  • Success comes by going the extra mile, working the extra hours, and investing the extra time. The same is true for us. If we want to get to excel in any segment of life, a little extra effort can help. Our efforts can go a long way if we only work a little smarter, listen a little better, push a little harder, and persevere a little longer.
  • Making a difference in your work is not about productivity; it's about people. When you focus on others and connect with them, you can work together to accomplish great things.
  • Envision a goal you'd like to reach. Make it big enough to scare you a little. Now write down a plan for moving toward it. Create mini-goals within the big goal, to set yourself up for continual progress. And include some risks, too. Set yourself up for success.
  • Leaders build margins, not image. A leader may be forced to take unpopular stands for the good of the company. Popularity isn't bad, but decisions made solely on the basis of popular opinion can be devastating. So take courage and make the right though sometimes painful choices.