Javascript SDK 自动登录 [英] Javascript SDK automatically login
问题描述
我正在尝试从 Facebook 获取登录按钮,以便使用 Javascript SDK 在我的网站上工作.如果用户已登录,我会取回电子邮件地址.这行得通.但是在我有我的 Facebook 登录按钮的页面上,我也有我网站的正常登录.(用户名和密码).但是当我进入我的登录页面时,页面会自动加载 Facebook 按钮并出现 Facebook 登录对话框.但我不想自动加载这个按钮.我希望用户总是必须按下 Facebook 的登录按钮.这可能吗?
I'am trying to get the Login button from Facebook to work on my website using the Javascript SDK. If the user is logged on, I get the email address back. This works. But on the page where I have my Facebook Login button, I also have the normal login for my website. (username & password). But when I go to my login page, the page automatically loads the Facebook button and the Facebook login dialog appears. But I don't want to load this button automatically. I want that users always have to press the login button for Facebook. Is this possible?
这是我的代码:
<script>
window.fbAsyncInit = function() {
FB.init({
appId: 'MY_APP_ID', // App ID
status: true,
cookie: true,
xfbml: true,
oauth: true,
});
FB.getLoginStatus(function(response) {
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me', function(response) {
alert('Good to see you, ' + response.email + '.');
});
} else {
alert('User cancelled login or did not fully authorize.');
}
});
});
// Additional initialization code here
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>
推荐答案
FB.getLoginStatus
方法用于判断用户是否登录
FB.getLoginStatus
method is used to know whether user has logged in or not
FB.login
用于提示登录页面对应用进行认证和授权.
FB.login
is used to prompt login page to authenticate and authorize the app.
您正在调用 FB.getLoginStatus 方法,而不必担心响应并始终触发 fb.login 按钮.
you are calling FB.getLoginStatus method and not bothering about the response and firing fb.login button always.
如果您总是希望用户单击登录按钮,请不要使用 FB.getLoginStatus
If you always want user to click login button don't use FB.getLoginStatus
使用此代码
<html>
<head>
<title>My Facebook Login Page</title>
</head>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'YOUR_APP_ID', // App ID
channelUrl : '//WWW.YOUR_DOMAIN.COM/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
});
};
// 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));
</script>
<div class="fb-login-button">Login with Facebook</div>
</body>
</html>
仅供参考:https://developers.facebook.com/docs/guides/web/
检查身份验证部分
FYI: https://developers.facebook.com/docs/guides/web/
check authentication section
这篇关于Javascript SDK 自动登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!