skip to Main Content

I’m trying to alert the access_token with a simple function but I’m getting undefined I’m following their syntax but maybe I’m doing something wrong?

javascript

   function logMe() {  
    FB.login(function(){
      FB.api('/me/accounts', {fields: 'access_token'}, function(response) {
      var accessToken = response.data.access_token;
      alert(accessToken);
        } );
    }, {scope: 'manage_pages'});
    }

2

Answers


  1. Chosen as BEST ANSWER

    With luschn response above this is the final answer

       function logMe() {
        var page_id=document.getElementById('page_id').value;
        FB.login(function(){
          FB.api('/' + page_id + '?fields=access_token', function(response) {
           //get access token
           var accessToken = response.access_token;
          console.log(accessToken);
            } );
        }, {scope: 'manage_pages'});
        }
    

  2. With /me/accounts, you get an array of pages you manage – not just one entry:

    FB.api('/me/accounts', {fields: 'access_token'}, (response) => {
        if (response && response.data) {
            for (let i = 0, count = response.data.length; i < count; i++) {
                console.log(respose.data[i].access_token);
            }
        }
    });
    

    This would be the API endpoint to get the access token for a specific page:

    /[page-id]?fields=access_token
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search