Priya Sunanthan
Priya Sunanthan

Reputation: 439

How to get the page access token in facebook

I am creating my first app in facebook. In that i am trying to access the user's page account details. I used this code:

FB.getLoginStatus(function(response) {
          if (response.status === 'connected') {
            alert('connected');
            var uid = response.authResponse.userID;
            var accessToken = response.authResponse.accessToken;
            getusername();              

}

Here i can able to get the user's main account id ,access_token, name everything. For getting the user's page account details i used this code

function getusername(){
    FB.api('/me/accounts', function(response){
            var p_accessToken = response.accessToken;
            var p_name = response.name;
            alert('The pagename is:'+ p_name + 'Page access token is' + p_accessToken);

        });

But it alerted the name and access_token as undefined. I dont know how to access the name, access_token of the page.

I think I need to change this p_accessToken =response.accessToken line.

But i dont know how to get this. Can anyone help me .

Upvotes: 2

Views: 4028

Answers (1)

Anvesh Saxena
Anvesh Saxena

Reputation: 4466

There was an issue with your code, I have corrected it

<script> 
FB.login(function(response){ 
FB.api('/me/accounts', function(response){ 
   var p_accessToken = response.data[0].access_token; 
   var p_name = response.data[0].name; 
   alert('The pagename is:'
           + p_name + 'Page access token is' 
           + p_accessToken);});}
   ,{scope:"manage_pages"}); 
</script>

the data is an array with your accounts being on different index numbers also its access_token instead of accessToken

Upvotes: 2

Related Questions