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