0

At first I am sorry for post the old post, but I don't know why I didn't make it correctly.

I post new feed into facebook fan page as admin. I have search many threads before and I fixed as they comments.

  • I have correct permissions for post page.
  • I have PAGE access_token for page.
  • I have tried logged out and in many times.
  • I have tried post feed in page manually as page admin (Successful).

But I still post as visior not admin in page.

Those are my functions:

    var page_id = [
                      '88290XXXXXX39694', //fan page 1
                      '17537XXXXXX02244', //fan page 2
               ];   
      
    var permission = 'publish_actions, publish_pages, manage_pages, user_events, publish_stream';
    
    function postFB(id, title, desc) {
    
        var title = title;
        var desc = desc;
        var body = title+"/n"+desc;
        var video_link = 'https://youtu.be/'+id+'';
    
        for(var i = 0; i<= page_id.length; i++ )
        {
            var accessToken = "";
            
            FB.api('/'+page_id[i]+'?fields=access_token', function(response) {
                accessToken = response.access_token;
            });
    
            FB.api('/'+page_id[i]+'/feed', 'post', {
                message: body,
                link: video_link,
                access_token: accessToken
            }, function(response) {
                if (!response || response.error) {
                    console.log(response);
                } else {
                    alert('Post ID: ' + response.id);
                }
            });
        }
    };
    
    function publish(id, title, decs) 
    {
        FB.login(function(response) {
            if (response.authResponse) {
                //Log auth permissions (in the response)
                console.log(response);
                FB.api('/me', function(response) {
                    console.log('Successful login for: ' + response.name);
    //                document.getElementById('status').innerHTML =
    //                    'Thanks for logging in, ' + response.name + '!';
                });
    
                postFB(id, title, decs);
    
            } else {
                console.log('User cancelled login or did not fully authorize.');
            }
        }, {
            scope: permission,
            return_scopes: true
        });
    };

Any suggesstion for my issue?

andyrandy
  • 72,880
  • 8
  • 113
  • 130
TommyDo
  • 663
  • 8
  • 23
  • why publish_stream? that permission is deprecated since many years. and why publish_actions? you don´t want to post as user, right? not sure why you ask for user_events either. only use permissions you really need. – andyrandy Oct 17 '16 at 09:19
  • It's not matter in my issues. I have already known this permission is no longer useful – TommyDo Oct 18 '16 at 02:26

1 Answers1

2

FB.api is asynchronous, you have to wait for the callback:

FB.api('/' + page_id[i] + '?fields=id,access_token', (response) => {
    FB.api('/' + response.id + '/feed', 'post', {
        message: body,
        link: video_link,
        access_token: response.access_token
    }, (response) => {
        if (!response || response.error) {
            console.log(response);
        } else {
            console.log('Post ID: ' + response.id);
        }
    });
});

The only permissions you need are manage_pages and publish_pages. I have also used arrow functions (because ES6 is here).

There is another big problem with the loop, you are using the "i" variable in the API calls - since the callback is asynchronous, it will not be the same when it actually hits the callback function. You should not mass-post in a loop anyway, make sure it works for ONE page.

andyrandy
  • 72,880
  • 8
  • 113
  • 130
  • As your comment, I figured out that didn't get the access_token before and I edited like yours. But the result is same. It still post to Visitor page instead of Admin timeline. Anyway thank for your suggesstion. – TommyDo Oct 18 '16 at 02:24
  • then you are not using the correct page token. debug it and make sure it is one. – andyrandy Oct 18 '16 at 08:30
  • I have debug the key token also. It returned correct token with my page ID. I think that I didn't publish the app with request permission so that I can't post as the admin page. Know I am waiting for request answer for this. – TommyDo Oct 18 '16 at 08:44
  • you don´t need to publish the app...but i just saw that you are trying to post in a loop. careful with the variables there, it´s definitely still an async problem. make sure you understand what´s happening with "i" in the async callback, debug it. – andyrandy Oct 18 '16 at 08:55
  • you should not mass-post on multiple pages anyway, looks spammy to me... – andyrandy Oct 18 '16 at 08:56
  • research "promises", they might help you there. you really need to make sure you understand async programming, it´s pretty important in javascript. – andyrandy Oct 18 '16 at 09:01
  • I understand your warning. I did't find out the reasons are...but I will consider your said carefully again. About the loop, the customer has more than 10 fan pages with varius languages. They want to post to the facebook pages in once click. Do you have any better way to handle this? – TommyDo Oct 18 '16 at 09:07
  • You are right. I delete the loop statement and leave single page ID run only. That 's OK to post as Admin, but as the customer request, if I didn't use the loop statement, how can I post into pages just by one click. – TommyDo Oct 18 '16 at 09:23
  • take a look at that other thread, the duplicate one. there are several important ways to solve this: promises, generators, async, ... that should give you enough input to do some programming/testing. – andyrandy Oct 18 '16 at 09:40
  • well, in this case it would even be a lot easier, i will update my answer – andyrandy Oct 18 '16 at 09:41
  • done. that should work. also updated the functions to arrow functions. – andyrandy Oct 18 '16 at 09:42
  • I confirmed that is WORKING. You are genius. Sorry for my stupid, my customer's deadline is too near so I have stress with this. Anyway, thank you sooooooooooooooo much. – TommyDo Oct 18 '16 at 10:08