Login with Facebook, grab access token

2019-08-25 18:57发布

问题:

I just need to login to Facebook and print/store the Access Token given in the Oauth response. I'm using the following code to initiate login:

    <script>
    (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.net/en_US/sdk.js#xfbml=1&version=v2.9&appId=ID_REDACTED";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
    </script>
    <div class="fb-login-button" data-max-rows="1" data-size="large" data-button-type="continue_with" data-show-faces="false" data-auto-logout-link="false" data-use-continue-as="false"></div>

When I load the page and click the "Continue With Facebook" button and allow the app access via the popup, an OAuth request is sent to Facebook.com and in the response, all of my data is include (including the Access Token). I can't figure out how to access this data since it's not sent to my server.

Any help is appreciated, thank you.

回答1:

Please see documentation for getLoginStatus(): https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus

The response object in the callback will have this info