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



我曾经有这样的代码,当用户访问我的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的更新方法?

谢谢!

让我们先修复代码:

<?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()将为您构建登录URL

相关内容

最新更新