不再要求用户接受新用户的申请?

时间:2011-08-26 16:22:59

标签: php facebook authentication login

我曾经有过这样的代码,一旦他们进入我在facebook上的应用程序页面,就会将用户发送到正常的应用程序接受页面:

<?php
$auth_url = "http://www.facebook.com/dialog/oauth?client_id=" 
. $app_id . "&redirect_uri=" . urlencode($canvas_page)."&scope=email,user_photos,friends_photos";
$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>");
} else {
    echo ("Welcome User: " . $data["user_id"]);
}
?>

使用新的SDK我用以下内容替换了所有内容:

<?php
require_once('src/facebook.php');
// Create our application instance
// (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));

// Get User ID
$user = $facebook->getUser();

// We may or may not have this data based 
// on whether the user is logged in.
// If we have a $user id here, it means we know 
// the user is logged into
// Facebook, but we don’t know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}

// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();

} else {
$loginUrl = $facebook->getLoginUrl(array('scope'=>'email,user_photos,friends_photos'));
}
?>

我的问题是,我是否还需要正确页面的顶级代码,询问用户是否需要授予我的应用授予访问权限?只有底部代码,这不再发生了?我有新的方法来使用新的SDK吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

让我们先修复代码:

<?php
require_once('src/facebook.php');
// Create our application instance
// (replace this with your appId and secret).
$facebook = new Facebook(array(
    'appId' => $app_id,
    'secret' => $app_secret,
));
$canvas_page = "YOUR_URL_HERE";
// Get User ID
$user = $facebook->getUser();

// We may or may not have this data based 
// on whether the user is logged in.
// If we have a $user id here, it means we know 
// the user is logged into
// Facebook, but we don’t know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

if ($user) {
    try {
    // Proceed knowing you have a logged in user who's authenticated.
        $user_profile = $facebook->api('/me');
        echo '<pre>'.htmlspecialchars(print_r($user_profile, true)).'</pre>';
    } catch (FacebookApiException $e) {
        error_log($e);
        $user = null;
    }
} else {
    echo "<script>top.location.href='" . $facebook->getLoginUrl(array('scope'=>'email,user_photos,friends_photos', 'redirect_uri'=>$canvas_page)) . "';</script>";
}
?>

注意:

  • 您需要确保正确链接到SDK
  • 使用$facebook->getUser()会检查signed_request以及当前用户的其他来源
  • getLoginUrl()将为您构建登录网址