我正在尝试将facebook login button
合并到我网站的主页中。我已将其与应用相关联,因此如果用户已注册,则我有app id
和secret
。
我使用了来自facebook的JavaScript SDK
但是当我按下facebook的登录按钮时,在授权之后,没有任何反应,我的网站的主页再次显示。有谁知道如何从Javascript SDK获取user's email
?我想在登录后重定向到另一个页面,但登录后它只停留在该页面上。
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function()
{
FB.init({
appId : '178124332351909', // App ID
channelUrl : 'channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.Event.subscribe('auth.authResponseChange', function(response)
{
if (response.status === 'connected')
{
alert("connected");
testAPI();
}
else if (response.status === 'not_authorized')
{
alert("nt connected");
FB.login();
}
else
{
alert("else");
FB.login();
}
});
};
// Load the SDK asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
// Here we run a very simple test of the Graph API after login is successful.
// This testAPI() function is only called in those cases.
function testAPI()
{
alert("in testapi");
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response)
{
console.log('Good to see you, ' + response.name + '.');
});
}
</script>
请帮助您重定向到另一个页面。
答案 0 :(得分:0)
要获得email
的许可,请将Fb.login()
替换为:
FB.login(function(response)
{
if(response.status == 'connected'){
alert('I am connected')
}
},{scope: 'email'});
要在成功登录后将用户重定向到其他页面,只需在testAPI()
函数中写入重定向代码:
function testAPI()
{
alert("in testapi");
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response)
{
console.log('Good to see you, ' + response.name + '.');
});
window.location="http://www.newlocation.com"; // HERE
}
答案 1 :(得分:0)
可能有以下原因:
如果您想让用户收到电子邮件,那么您可以将其作为“perms”参数传递给您。
<div class="fb-login-button" onclick="fbLoginClick();" perms="email"><div>