I needed to implement oAuth for the the Facebook graph API today and couldn't find any straight forward php examples so I thought I would post this in case any else is in a similar situation.
The basic process for the authentication is outlined here so I won't go into the details of it. In the example below the bit you'll need to take note of is the "scope" parameter in the link, this defines the permissions you are requesting. To see a full list of available permissions click here.
You'll also need to sign up for an API key first and register your application with Facebook, you can sign up here.
Step 1:
In your page make a link like this:
<a href="https://www.facebook.com/dialog/oauth?client_id=<?=FACEBOOK_APP_ID?>&redirect_uri=
<?=urlencode('http://yoursite.com/fb_oauth_return.php')?>
&scope=offline_access,user_checkins,friends_checkins">Connect with Facebook</a>
Step 2:
Create a page called fb_oauth_return.php which contains the following code
And that's it. If you requested offline access you'll need to save the token to your database so that you can use it to make requests on the user's behalf later.
The basic process for the authentication is outlined here so I won't go into the details of it. In the example below the bit you'll need to take note of is the "scope" parameter in the link, this defines the permissions you are requesting. To see a full list of available permissions click here.
You'll also need to sign up for an API key first and register your application with Facebook, you can sign up here.
Step 1:
In your page make a link like this:
<a href="https://www.facebook.com/dialog/oauth?client_id=<?=FACEBOOK_APP_ID?>&redirect_uri=
<?=urlencode('http://yoursite.com/fb_oauth_return.php')?>
&scope=offline_access,user_checkins,friends_checkins">Connect with Facebook</a>
Step 2:
Create a page called fb_oauth_return.php which contains the following code
<? if(!isset($_GET["error"])) { if(isset($_GET["code"])) { $code = $_GET["code"]; $url = 'https://graph.facebook.com/oauth/access_token?client_id='.FACEBOOK_APP_ID.'&redirect_uri='.urlencode('http://yoursite.com/fb_oauth_return.php').'&client_secret='.FACEBOOK_SECRET.'&code='.$code; $curl_handle=curl_init(); curl_setopt($curl_handle,CURLOPT_URL,$url); curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,6); curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1); $buffer = curl_exec($curl_handle); curl_close($curl_handle); if(strpos($buffer, 'access_token=') === 0) { //if you requested offline acces save this token to db //for use later $token = str_replace('access_token=', '', $buffer); //this is just to demo how to use the token and //retrieves the users facebook_id $url = 'https://graph.facebook.com/me/?access_token='.$token; $curl_handle=curl_init(); curl_setopt($curl_handle,CURLOPT_URL,$url); curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2); curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1); $buffer = curl_exec($curl_handle); curl_close($curl_handle); $jobj = json_decode($buffer); $facebook_id = $jobj->id; } else { //do error stuff } } } else { //do error stuff } ?>
And that's it. If you requested offline access you'll need to save the token to your database so that you can use it to make requests on the user's behalf later.