How to: Check if User Has Certian Permission – Facebook API
There are two three ways to check if the user has granted your application a specific Permission:
Using the Graph API
Now that Facebook added the permissions connection to the user object, you can easily check the user’s permission:
$permissions = $facebook->api("/me/permissions");
if( array_key_exists('publish_stream', $permissions['data'][0]) ) {
// Permission is granted!
// Do the related task
$post_id = $facebook->api('/me/feed', 'post', array('message'=>'Hello World!'));
} else {
// We don't have the permission
// Alert the user or ask for the permission!
header( "Location: " . $facebook->getLoginUrl(array("scope" => "publish_stream")) );
}Back to topUsing the FQL permissions table
You can query the permissions table to check if a certain permission (or set of permissions) is granted:
SELECT read_stream FROM permissions WHERE uid=me()
We will give two examples of how to query this table in:
- PHP:
$perms = $facebook->api(array( "method" => "fql.query", "query" => "SELECT read_stream,offline_access,publish_stream FROM permissions WHERE uid=me()" )); echo "<ul>"; foreach($perms[0] as $k=>$v) { echo "<li>"; if($v === "1") { echo "<strong>$k</strong> permission is granted."; } else { echo "<strong>$k</strong> permission is not granted."; } echo "</li>"; }Output:
- read_stream permission is not granted.
- offline_access permission is not granted.
- publish_stream permission is granted.
- Javascript:
FB.api({ method: 'fql.query', query: 'SELECT read_stream,offline_access,publish_stream FROM permissions WHERE uid=me()' }, function(resp) { for(var key in resp[0]) { if(resp[0][key] === "1") console.log(key+' is granted') else console.log(key+' is not granted') } });
Using the REST API users.hasAppPermission method
It is worth mentioning that the users.hasAppPermission is still working and can be called from the Facebook PHP-SDK like so:
$isGranted = $facebook->api(array( "method" => "users.hasAppPermission", "ext_perm" => "publish_stream", "uid" => 579187142 )); if($isGranted === "1") echo "Permission granted!";
Please note that this call doesn’t require the uid parameter, if you have a valid user session.
Now we call the same function in Javascript:
FB.api({ method: 'users.hasAppPermission', ext_perm: 'publish_stream' }, function(resp) {
if (resp === "1") {
alert('Permission granted');
} else {
alert("Permission not granted");
}
});
Back to topAdvertisment
Recent Tutorials
- How To: Create A User Photo Albums Browser Using Facebook Graph API
- How To: Upload A Photo To A User’s Profile Using Facebook Graph API
- How To: Check Status And RSVP To Facebook Events Using Graph API & FQL
- Facebook Javascript SDK Best Practices
- How To: Create Facebook Events Using Graph API – Advanced







Pingback: How To: Ask For Extended Permission In Your Facebook Application | API? Yes Master!