Using OAuth and Graph API’s in FaceBook Canvas Apps

On a FaceBook Canvas Application project I needed to post messages to a users wall using PHP. Now, you may or may not be aware, but FaceBook has switched to Open Authentication and Graph API’s. There is quote a bit of documentation on the subject and it’s implementation at http://developers.facebook.com/docs/. The only thing missing is direct examples on how to perform these tasks. I searched around a bit and found information on this topic scarce. So, for your viewing pleasure, here are a couple examples on how to use the OAuth and Graph API’s.
Requesting Application Permissions
To request “extended permissions” from a user along with any other capabilities you’d like you app to be granted, you can call the following URL, either directly or through a CURL call.
https://graph.facebook.com/oauth/authorize?client_id=XXXXXXXXXXXXXXX&redirect_uri=http://apps.facebook.com/myapp&scope=publish_stream
You can locate you Client ID in you developer application settings. The “redirect_uri” is obviously where you’d like to redirect when the user makes a decision on your request. The Note the “scope” parameter. This is where you specify a coma delimited list of permissions you’d like you app to request. For a full list of available permissions take a look here:
http://developers.facebook.com/docs/authentication/permissions
Once redirected, some data is passed as URL params about you’re users choice. You can use this in you app to whatever ends you need.
Posting to a User’s Wall
There are two parts to doing this. First you need to get an Access Token before you can actually make the call to post to a user’s wall. Here you can see the call through OAuth to retrieve the token. Again, you’re applications Client ID and Client Secret can be found in your developer application settings. The callFb is just a CURL method wrapper that returns the results of the loaded page. After we get the contents returned, we just extract the actual token.
{
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_POSTFIELDS => http_build_query($params),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_VERBOSE => true
));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$params=array('client_id'=>'XXXXXXXXXXXXXXX', 'type'=>'client_cred', 'client_secret'=>'XXXXXXXXXXXXXXX');
$url = "https://graph.facebook.com/oauth/access_token";
$access_token = callFb($url, $params);
$access_token = substr($access_token, strpos($access_token, "=")+1, strlen($access_token));
Now that we have our token, we can use callFb again to tap the Graph API and post to the user’s wall.
$params=array('access_token'=>$access_token, 'message'=>$message, 'link'=>'http://apps.facebook.com/myapp', 'name'=>'Graph API Test App');
$url = "https://graph.facebook.com/PROFILE_ID/feed";
callFb($url, $params);
You can see the message, name, and link being created. Notice the “/me” in the URL. This is a short cut to posting to the currently authenticated user. This can also be replaced with a specific user’s FaceBook ID as well. The next to notice is the “/feed” in the URL. This is the type of posting you’d like to perform. There are a number of other options to choose from and you can find them at the folowing link under “Publishing to Facebook”:
http://developers.facebook.com/docs/api
That’s it! I hope this saves someone from spending hours trying to locate some examples of how to make these common calls in a FaceBook app. If anyone has any further insight on better wasy to perform these task, please leave a comment.