To post on facebook page using javascript SDK with page access token

560 Views Asked by At

I am trying to post to my facebook's wall using javascript SDK. I am first getting user access token. Then using user access token i am getting page access token and including it while posting to a wall. I'm getting following error enter image description here

here's my code : First am trying to login using FB.login

FB.login(function(response) {
        if (response.authResponse) {
            console.log('Welcome!  Fetching your information.... ');
            FB.api('/me', function(response) {
                console.log('Good to see you, ' + response.name + '.'+JSON.stringify(response));
            });
        } else {
            console.log('User cancelled login or did not fully authorize.');
        }
    },{scope: 'manage_pages,publish_pages'});

Then trying to post to a page.

var body = 'Reading JS SDK documentation';
FB.getLoginStatus(function(response) { 
               console.log('login status',response);
            if(!(response.status === 'connected')){
                location.href = './fb-login.html';
                } else {
                    uID = response.authResponse.userID;
                    accessToken = response.authResponse.accessToken;
                    console.log('accesstoken::',response.authResponse.accessToken);
                    FB.api('/me', {fields: 'last_name'}, {  access_token : accessToken } ,function(response) {
                        console.log(response);
                    });
                    //get list of pages
                    if(accessToken!=null){
                        FB.api('/me/accounts','get',{  access_token : accessToken },function(response){
                            console.log('resp of pages',response);
                            if(response!=null){
                                var data = response.data;
                                pageAccessToken= data[0].access_token;
                                console.log('pageAccessToken::',pageAccessToken);

                                FB.api('/6599048*******/feed', 'post', {message :body, access_token : pageAccessToken }, function(response) {
                                    console.log('response',response)
                                    if (!response || response.error) {
                                        alert('Error occured');
                                    } else {
                                        alert('Post ID: ' + response.id);
                                    }
                                });
                                }

                        });
                    }
0

There are 0 best solutions below