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")) );
}

 

Using 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[0as $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')
        }
    });
    Back to top

 

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");
    }
});

 

 

 

 

 

 

 

 

 

总结:项目当中,想通过facebook提供的php版本的SDK发送feed,应用的就是以下代码:

 

$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")) );
}

 

 

 

 

 

 

 

 

 

 

 

posted on 2011-07-06 17:53  小炒花生米  阅读(1072)  评论(0编辑  收藏  举报