如何向我当前的Facebook应用用户请求额外的许可?

时间:2011-10-22 14:11:54

标签: php facebook facebook-graph-api

我正在使用此代码向用户请求我的应用的权限

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

但现在我需要向CURRENT用户询问“publish_stream”权限, 我在scope参数中添加了“publish_stream”权限,但对于之前已经为应用程序授予权限的用户来说,它不起作用。

所以我该如何解决这个问题呢?

感谢...

3 个答案:

答案 0 :(得分:4)

显然,一旦用户授权您的应用,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, '-_', '+/'));
}
?>

我的教程可以找到更多内容:How to: Check if User Has Certian Permission – Facebook API

答案 1 :(得分:0)

我很确定一旦你向你的作用域添加你要求的新权限,facebook就会通过应用程序的请求访问对话框自动再次提示它们,用户只需要批准它们。

答案 2 :(得分:0)

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