Facebook登录JS SDK - getLoginStatus总是返回未知

时间:2017-09-18 15:17:23

标签: javascript facebook facebook-graph-api facebook-javascript-sdk facebook-login

当我尝试使用JavaScript登录Facebook时,并希望获得登录状态时,我会收到“未知”的回复。我阅读了所有Facebook的文档和很多帖子,但我仍然遇到同样的错误。

我正在使用谷歌浏览器Versión61。

这是我的代码:

(function(d, s, id){
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) {return;}
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.com/en_US/sdk.js";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

window.fbAsyncInit = function() {
   FB.init({
      appId      : appID,
      xfbml      : true, // parse XFBML
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      version    : 'v2.10'
   });

   FB.AppEvents.logPageView();

   // Get current login url
   var url = new URL(window.location.href);
   // Get if the user has been connected via Facebook
   var conn = url.searchParams.get("connected");
   // Check the var
   if(conn == "true" || conn == true){
     loginWithFacebook(null);
   }
};

// Login with Facebook
function loginWithFacebook(elementButton){  
  // Javascript URL    
  var strUri = encodeURI('https://vinglet.com/login?connected=true'),
      myPermissionLogIN = 'email,public_profile';
  // Check if the user has initialized session on the app
  FB.getLoginStatus(function(response) {
      // Check the status of the user
      if (response.status !== 'connected'){
      window.location.href = 'https://graph.facebook.com/oauth/authorize?client_id='+appID+'&scope='+myPermissionLogIN+'&redirect_uri='+strUri;
   }
   else{
      var meObject = {
          fields: `first_name, last_name, email, public_key, 
            middle_name, link, gender, birthday, about`
      };
      // Save the data into the DB
      loginWithFacebookAJAX(meObject,elementButton);
   }
  });    
}

0 个答案:

没有答案
相关问题