2011-11-14 164 views
0

我有一个很难得到Facebook的JS API来为http://developers.facebook.com/docs/reference/javascript/的Facebook要求用户登录,即使他们已经登录

具体描述工作,用户似乎得到要求登录即使他们已经登录到Facebook。对于这个我做了以下内容:

<div id="fb-root"></div> 
<script> 
    window.fbAsyncInit = function() { 
    FB.init({ 
     appId  : '3120012412', // App ID goes here 
     channelURL : '//WWW.qy.ORG/channel.php', // 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) { 
     alert('Welcome! Fetching your information.... '); 
     FB.api('/me', function(response) { 
     alert('Good to see you, ' + response.name + '.'); 
     alert('response' + response); 

     FB.logout(function(response) { 
     alert('Logged out.'); 
     }); 
    }); 
    } else { 
     alert('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> 

我也试过:

FB.getLoginStatus(function(response) { 
    if (response.authResponse) { 
    // logged in and connected user, someone you know 
    } else { 
    // no user session available, someone you dont know 
    } 
}); 

,当我在Facebook的我已经登录这也失败均匀。

如果我想让用户通过最低限度的麻烦,只需基本访问公共信息,该怎么办?我明白他们必须授权基本访问权限,但是不应该一再询问他们是否应该这样做?

回答

0

您的第一个代码块正在将用户登录,然后立即再次将其注销 - 因此,如果您已经运行了几次,您可能未登录。删除FB.logout块并再次尝试,看看它是否有所作为。

要记住的另一件事(可能是你的第二个例子不工作的原因)是虽然你登录到facebook.com,你必须通过FB.login登录到你自己的域名。 getLoginStatus将检查您的fbsr_cookie并考虑您是否登录,但您必须已经拥有该cookie,首先请拨打FB.login

因此,我建议使用这样的:

// Additional initialization code here 
    FB.login(function(response) { 
     if (response.authResponse) { 
      alert('Welcome! Fetching your information.... '); 
      FB.api('/me', function(response) { 
      alert('Good to see you, ' + response.name + '.'); 
      alert('response' + response); 
      }); 
     } else { 
      alert('User cancelled login or did not fully authorize.'); 
     } 
    }, {scope: 'email'}); 
+0

非常感谢您的帮助! –

+0

这是否解决了您的问题?如果有答案,请接受答案,否则请留下更多信息。 :) – Abby

相关问题