Очевидно, что как только пользователь авторизует ваше приложение, 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, '-_', '+/'));
}
?>
Более подробную информацию можно найти в моем уроке: Как: проверить, имеет ли пользователь разрешение Certian - API Facebook