Using the Graph API to Get Permissions Granted By Facebook user

Before Graph Api you could very easily get the permissions that the user granted the App. It was not available in Graph Api. Recently Facebook added new functionality to do this in graph api. Here is code to get the permissions of a use


  $app_id = "FACEBOOK_APP_ID";
  $app_secret = "FACEBOOK_SECRET_KEY"; 
  $my_url = "YOUR_POST_LOGIN_URL"; 

  $code = $_REQUEST["code"];
  
  if(empty($code)) {
    $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" 
    . $app_id . "&redirect_uri=" . urlencode($my_url) . "&scope=email";

    echo("<script>top.location.href='" . $dialog_url . "'</script>");
  }

  $token_url = "https://graph.facebook.com/oauth/access_token?client_id="
    . $app_id . "&redirect_uri=" . urlencode($my_url) 
    . "&client_secret=" . $app_secret 
    . "&code=" . $code;

  $access_token = file_get_contents($token_url);
  $graph_url="https://graph.facebook.com/me/permissions?".$access_token;
  echo "graph_url=" . $graph_url . "<br />";
  $user_permissions = json_decode(file_get_contents($graph_url));
  print_r($user_permissions);

or if you use the php sdk, you can use this code sinnept


$facebook = new Facebook(array(
  'appId'  => FACEBOOK_APP_ID,
  'secret' => FACEBOOK_SECRET_KEY,
  'cookie' => false,
));

$session = $facebook->getSession();

$loginUrl = $facebook->getLoginUrl(
	array(
		'canvas'=> 1,
		'fbconnect' => 0,
		'req_perms' => 'manage_pages,offline_access,user_likes,read_stream'		
	)
);

$me = null;
$chk=0;
if ($session) {

	try {
		$uid = $facebook->getUser();
		$me = $facebook->api('/me');
		$permissions = $facebook->api('/me/permissions');
		if(count($permissions['data'][0])<5)
			$chk=1;

	} catch (FacebookApiException $e) {
		//error_log($e);
	}
}
if (!$me or $chk==1) {
	echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
}

About Zakir Hyder

This entry was written by .

05. May 2011 by Zakir Hyder
Categories: Facebook, Graph Api, PHP, Social Networking Application Development | Tags: , , | Comments

Comments

  1. […] Using the Graph API to Get Permissions Granted By Facebook user Thursday, May 05, 2011 12:46 PM Before Graph Api you could very easily get the permissions that the user granted the App. It was not available in Graph Api. Recently Facebook added new functionality to do this in graph api. Here is code to get the permissions of a use or if you use the php sdk, you can use this […] […]