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