JS SDK FB.login() works but pop-up dialog is staying open after logged in

后端 未结 6 1763
清酒与你
清酒与你 2021-02-18 20:48

I am using the JS SDK for Facebook based on the NEW GraphAPI for Auth/Login.

Has anyone had this issue when logging in after FB.login() was called via the J

6条回答
  •  不知归路
    2021-02-18 20:54

    I struggled with this issue recently. The problem appeared from no where, presumably from some change in the Facebook JS SDK. For what its worth I plan to never use the JS SDK again, these random issues eat up my time.

    Anyway here is the hack that I used to get around the issue.

    var accessToken, fb_response;
    
    if (window.location.hash.length < 30) {
      window.location = "http://www.facebook.com/dialog/oauth?client_id=YOUR_ID&redirect_uri=YOUR_URL&scope=YOUR_PERMISSIONS&response_type=token";
    } else {
      fb_response = window.location.hash;
      accessToken = fb_response.substr(14, fb_response.indexOf("&expires") - 14);
      FB._authResponse = {
        accessToken: accessToken
      };
      window.location.hash = "";
      FB.api("/me", function(profile) {
        if (profile.id) {
           app_init();
        } else {
           alert("Problem connecting to Facebook");
        }
      }); 
    }
    

    Basically I send the user to the Facebook oauth dialog and when they return I grab the access token from the hash. I then set the internal access token parameter on the Facebook Object. Once this is done you can make all the normal Facebook Api calls.

    Hopefully this helps someone! For future projects I will definitely stick to a server side auth flow, you have a lot more control!

提交回复
热议问题