【问题标题】:Authorization window reopens then quickly closes even if already authorized. How to avoid this?即使已经授权,授权窗口也会重新打开然后快速关闭。如何避免这种情况?
【发布时间】:2011-10-14 04:03:06
【问题描述】:

我在 Facebook 上的应用程序使用 JavaScript SDK。当用户导航到我的应用程序页面并转到我的应用程序时,弹出窗口要求他们授权该应用程序。这很好用。

但是,如果他们授权该应用程序,然后稍后再返回,另一个弹出窗口(我相信这是另一个授权窗口)将快速打开然后关闭。

我的代码在做什么?代码如下。

<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '221953271200525', // App ID
      channelURL : '//www.vidjahgames.com/fall/channel.html', // Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      oauth      : true, // enable OAuth 2.0
      xfbml      : true  // parse XFBML
    });

    // Additional initialization code here


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 + '.');

     });
   } else {
     console.log('User cancelled login or did not fully authorize.');
   }
 }, {scope: 'email'});


  };

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

【问题讨论】:

    标签: javascript facebook authorization


    【解决方案1】:

    FB.login 使弹出窗口出现,因此您应该首先知道用户是否已登录,如果未登录,则显示登录弹出窗口:

    FB.getLoginStatus(function(response) {
      if (response.session) {
       // A user has logged in, and a new cookie has been saved
       FB.api('/me', function(response) {
            _userName = response.name;
            alert("hello"+_userName);
         }.bind(this));
     } else {
       FB.login(function(response) {
          // the rest of your code here...
       }
    
     }
    });
    

    【讨论】:

      猜你喜欢
      • 2022-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多