Saturday, January 15, 2011

How to submit facebook status via OAuth.

You should read my last 2 previous post before this to know how the OAuth process works.
This post will teach you how to submit facebook status using OAuth. just follow the simple steps below with the complete code at the end for your implementation.

1. Same with my previous post, you need to have an app ID, site URL, and app Secret to submit facebook status on users profile. just for this testing, you can use the details below from the facebook app "Hotshots Point Of View".

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

2. After having the 3 details, you should have to request permission for your app to be able to access users profile, but this time an added permission to post status. to do that, an additional parameter SCOPE=user_photos,user_videos,publish_stream should be added on your authorize URL. again, app ID will be your client_id, and site URL will be your redirect_uri. please see below authorize URL with the permission to post status on users profile.

https://graph.facebook.com/oauth/authorize?client_id=182635521758593&redirect_uri=http://localhost/facebook/&scope=user_photos,user_videos,publish_stream

3. User should click the URL above and authorize your application to access profile and submit status. facebook will return to the redirect_uri http://localhost/facebook/ with the parameter CODE as part of query string. capture the CODE parameter and exchange it for an access TOKEN for the next step below.

4. To get an access token, you should pass the app ID, site URL, app Secret, and the CODE parameter value you get from the authorize URL. please see the access token URL below. you should replace the <CODE> with the CODE parameter you get from the authorize URL above.

https://graph.facebook.com/oauth/access_token?client_id=182635521758593&redirect_uri=http://localhost/facebook/&client_secret=495625ad928ea277548d0f423f420ef0&code=<CODE>

5. From the URL above, facebook will echo the access TOKEN which you will need to post facebook status. you will need to get the user id of the user you wish to post facebook status, and to do that, just replace the <TOKEN> with the access TOKEN on the URL below.

https://graph.facebook.com/me?access_token=<TOKEN>

6. Once you have the USER ID, and TOKEN, you can now finally post facebook status on users profile. execute the CURL script below with your facebook STATUS.

curl -F 'access_token=<TOKEN>' -F 'message=<STATUS>' https://graph.facebook.com/<USERID>/feed

For you to have a working script in submitting facebook status. please see below script that does all in 1 page: authorize app, access token, get user info, and post facebook status.

<?
$client_id = '182635521758593';
$redirect_uri = 'http://localhost/facebook/';
$client_secret = '495625ad928ea277548d0f423f420ef0';

$status = 'How to submit facebook status via OAuth - http://paulgonzaga.blogspot.com/2011/01/how-to-submit-facebook-status-via-oauth.html';
$code = $_GET['code'];

if ($code) {
        // get access token
        $oauthurl = "https://graph.facebook.com/oauth/access_token?client_id=$client_id&redirect_uri=$redirect_uri&client_secret=$client_secret&code=$code";
        $data = do_get_request($oauthurl);

        $ret = explode("&", $data);
        $token = preg_replace('/^access_token=/', '', $ret[0]);

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

        $ret = json_decode($data);
        $userid = $ret->id;

        $result = shell_exec("curl -F 'access_token=$token' -F 'message=$status' https://graph.facebook.com/$userid/feed");

        echo "done!</br>";
}

function do_get_request($get_url)
{
        $url_handler = fopen("$get_url", 'r');
        $url_contents = stream_get_contents($url_handler);
        fclose($url_handler);

        return $url_contents;
}

?>

<html>
<head>
</head>
<body>
<a href="https://graph.facebook.com/oauth/authorize?client_id=<?=$client_id?>&redirect_uri=<?=$redirect_uri?>&scope=user_photos,user_videos,publish_stream">authorize app</a>
</body>
</html>


Hope you like it!! happy coding.

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.