简体   繁体   中英

Facebook Graph API console error

I am trying to display the feed into my console using a console.log however it is returning an error of no token.

message: "An active access token must be used to query information about the current user." type: "OAuthException" error code : 2500;

the message above is what I am getting. I have followed the steps given by FB.

The code is bellow.

<script>

 window.fbAsyncInit = function() {
    FB.init({
      appId      : '',
      xfbml      : true,
      version    : 'v2.1',

    });
    FB.api('/me', {fields: 'last_name'}, function(response) {
    console.log(response);
     });

  };



  (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 = "http://connect.facebook.net/en_UK/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));   


</script>
<div id="fb-root">
</div>

You are trying to call the API without asking the user to login first. Instead, remove the FB.api line in your code and replace it with the below:

FB.login(function(){
  FB.api('/me', 'get', {fields: 'last_name'}, function (response ) {
    console.log(response);
  });
});

Ideally, you should call the above login code when a "Login" button is pressed, eg:

<button type="button" onclick="doLogin();">Login to Facebook</button>

(Remember to wrap the FB.login() function in the doLogin() function.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM