0
votes

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>
1
The user needs to login before you can call /me - WizKid
I have logged into facebook and got the access token, - user3182518
i don“t see any login code. how exactly did you login the user? and calling an api function right after init without checking if the user is logged in is a bad idea... - andyrandy
What does the code for login look like? And what access token? - WizKid
FB.getLoginStatus(function(response) { if (response.status === 'connected') { console.log('Logged in.'); } else { FB.login(); } }); - user3182518

1 Answers

0
votes

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, e.g.:

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

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