这是canvas教程中的代码http://developers.facebook.com/docs/appsonfacebook/tutorial/
获取授权代码,将用户重定向到画布页面,并在url中显示代码。
比如http://apps.facebook.com/yourapp/?code=1234thecode5678
所以,有可能做到这一点,使用ajax和获得代码没有重定向?而不是这个方法?
提前感谢
$app_id = "YOUR_APP_ID";
$canvas_page = "YOUR_CANVAS_PAGE_URL";
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page);
$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"]);
}
?>
你可以使用Facebook JS SDK来登录用户,而不需要刷新或重定向用户。
摘自FB.login主题下的Facebook JS SDK文档
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
FB.logout(function(response) {
console.log('Logged out.');
});
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, {scope: 'email'});
注意:如果你正在使用oAuth 2.0(你真的应该),这是实现。取自同一个url:
如果没有OAuth 2.0, scope应该替换为perms,和响应。authResponse被response.session取代。记住所有应用程序必须在2011年10月1日前迁移到OAuth 2.0。