如何从我目前的facebook应用程序用户请求额外的许可


How to ask for extra permission from my current facebook app users?

我使用这段代码请求用户允许我的应用程序

 $app_id = "1231654321654121";
 $canvas_page = "http://apps.facebook.com/manydldotnet/";
 $auth_url = "https://www.facebook.com/dialog/oauth?client_id=" . $app_id . "&redirect_uri=" . urlencode($canvas_page) . "&scope=email,read_stream";
 $signed_request = $_REQUEST["signed_request"];
 list($encoded_sig, $payload) = explode('.', $signed_request, 2); 
 $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
 if (empty($data["user_id"])) {
        echo("<script> top.location.href='" . $auth_url . "'</script>");
 }

但现在我需要问当前用户的"publish_stream"权限,我在scope参数中添加了"publish_stream"权限,但对于之前已经给应用程序权限的用户来说,它不起作用。

那么我如何解决这个问题呢?

谢谢…

显然,一旦用户授权你的应用程序,user_id将始终出现在signed_request中。你需要检索用户的权限并检查。

下面是一个例子:

<?php
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
$canvas_page = "http://apps.facebook.com/appnamespace/";
$GRAPH_URL = "https://graph.facebook.com/";
$scope = "publish_stream,email";
$auth_url = "https://www.facebook.com/dialog/oauth?client_id=" . $app_id . "&redirect_uri=" . urlencode($canvas_page) . "&scope=" . $scope;
$signed_request = $_REQUEST["signed_request"];
$data = parse_signed_request($_REQUEST["signed_request"], $app_secret);
if (empty($data["user_id"])) {
    echo("<script> top.location.href='" . $auth_url . "'</script>");
    exit;
}
$permissions = json_decode(file_get_contents($GRAPH_URL . "me/permissions?access_token=" . $data["oauth_token"]), TRUE);
if( array_key_exists('publish_stream', $permissions['data'][0]) ) {
    // Permission is granted!
    // Do the related task
    echo "You granted the publish_stream permission to my app!";
} else {
    // We don't have the permission
    // Alert the user or ask for the permission!
    echo("<script> top.location.href='" . $auth_url . "'</script>");
}
function parse_signed_request($signed_request, $secret) {
    list($encoded_sig, $payload) = explode('.', $signed_request, 2);
    // decode the data
    $sig = base64_url_decode($encoded_sig);
    $data = json_decode(base64_url_decode($payload), true);
    if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
        error_log('Unknown algorithm. Expected HMAC-SHA256');
        return null;
    }
    // check sig
    $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
    if ($sig !== $expected_sig) {
        error_log('Bad Signed JSON signature!');
        return null;
    }
    return $data;
}
function base64_url_decode($input) {
    return base64_decode(strtr($input, '-_', '+/'));
}
?>

更多可以在我的教程中找到:如何:检查用户是否有一定的权限- Facebook API

我敢肯定,一旦你添加了新的权限请求到你的范围,facebook会自动提示他们与应用程序的请求访问对话框,用户只需要批准他们。

<fb:login-button scope="create_event">Grant Permissions to create events</fb:login-button>