无法通过FB.getLoginStatus获取登录状态

时间:2012-09-04 10:16:35

标签: javascript facebook facebook-javascript-sdk

我无法让这个工作,我已经尝试了我在网上找到的所有解决方案。有人可以调查一下,让我知道我做错了什么。

$(document).ready(function(){
      window.fbAsyncInit = function() {
           FB.init({
                appId: 'xxxxxxx',
                cookie: true,
                xfbml: true,
                oauth: true
           });
           FB.Event.subscribe('auth.login', function(response) {
                window.location.reload();
           });
           FB.Event.subscribe('auth.logout', function(response) {
                window.location.reload();
           });
      };
      (function() {
           var e = document.createElement('script'); e.async = true;
           e.src = document.location.protocol +
           '//connect.facebook.net/en_US/all.js';
           document.getElementById('fb-root').appendChild(e);
      }());

      $("#fb_login_status_check").click(function(){
           FB.getLoginStatus(function(response) {
                           console.log(response);
                if (response.status === 'connected') {
                     // the user is logged in and has authenticated your
                     // app, and response.authResponse supplies
                     // the user's ID, a valid access token, a signed
                     // request, and the time the access token 
                     // and signed request each expire
                     var uid = response.authResponse.userID;
                     var accessToken = response.authResponse.accessToken;
                     console.log('User logged in and autenticated');
                  } else if (response.status === 'not_authorized') {
                     // the user is logged in to Facebook, 
                     // but has not authenticated your app
                     console.log('User logged in, but not autorized');
                  } else {
                     // the user isn't logged in to Facebook.
                     console.log('User not logged in');
                  }
           }, true);


      })

      $("#fb_logout_button").click(function() {
           if(FB.logout(function(response) {
                console.log('User logout.');
           })){
                console.log('User  logout.');
           }else{
           console.log('User didnt logout.');
           }
      });

      $("#fb_login_button").click(function() {
           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,user_likes'
           }); 
      });


      });

调用函数后我得到的响应是

authResponse null
status "unknown"

用户已登录,我可以使用php sdk获取所有信息。请帮忙,我试着在最近2天做这项工作......

1 个答案:

答案 0 :(得分:1)

首先应该初始化:

(function() {`   
        var e = document.createElement('script');
        e.async = true;
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';   
        document.getElementById('fb-root').appendChild(e);
}());

这是使用这些FB功能所需的facebook Javascript SDK。并且不要忘记页面上的'fb-root'元素!只需在<div id='fb-root'></div>代码后添加<body>