Javascript SDK自动登录 [英] Javascript SDK automatically login

查看:116
本文介绍了Javascript SDK自动登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从Facebook上获取登录按钮,使用Javascript SDK在我的网站上工作。如果用户登录,我将收到电子邮件地址。这个工作。但是在我拥有我的Facebook登录按钮的页面上,我也有我的网站的正常登录。 (用户名和密码)。但是当我进入我的登录页面时,页面会自动加载Facebook按钮,并显示Facebook登录对话框。但是我不想自动加载这个按钮。我希望用户总是按Facebook的登录按钮。这是可能的吗?



这是我的代码:

 脚本> 
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'函数(响应){
alert('Good to see you,'+ response.email +'。');
});
} else {
alert('User取消登录或未完全授权。');
}
});
});

//其他初始化代码在这里
};

//异步加载SDK
(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.login 提示登录页面来验证和授权应用程序。



您正在调用FB.getLoginStatus方法,而不是一直关注响应并触发fb.login按钮。



如果您一直希望用户点击登录按钮,请勿使用 FB.getLoginStatus



使用此代码

 < html> 
< head>
< title>我的Facebook登录页面< / 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,//检查登录状态
cookie:true,//启用Cookie以允许服务器访问会话
xfbml:true / /解析XFBML
});
};
//异步加载SDK
(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>使用Facebook登录< / div>

< / body>
< / html>

FYI: https://developers.facebook.com/docs/guides/web/

检查认证部分


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?

This is my code:

<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 method is used to know whether user has logged in or not

FB.login is used to prompt login page to authenticate and authorize the app.

you are calling FB.getLoginStatus method and not bothering about the response and firing fb.login button always.

If you always want user to click login button don't use FB.getLoginStatus

Use this code

<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>

FYI: https://developers.facebook.com/docs/guides/web/
check authentication section

这篇关于Javascript SDK自动登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆