简体   繁体   中英

Posting custom stories to Facebook with batch request

I have a web application that allows users to post custom stories to their Facebook timeline with the list of fitness exercises they have performed.

The first version is looping through the exercises and calling FP.api for each exercise and it works fine.

Now I would like to make a single call to FB.api with a batch request to speed up the posting and that's where I'm having trouble.

Here is the code with the loop that works fine (exids is an array of numbers):

function postToFB(exids)
{
    fbi = 0;
    fblength = exids.length;
    for (var i = 0; i < fblength; i++)
    {
        FB.api(
            'me/' + vitNamespace + ':perform',
            'post',
            {
                exercise: "http://www.vitalclub.net/scripts/getExforFB.php?exid=" + exids[i],
                'fb:explicitly_shared': true
            },
            function(response) {
                ...
            });
    }
}

and here is the code with the batch request that returns an error:

function postToFB(exids)
{
    var batcharr = [];
    for (var i = 0; i < exids.length; i++)
        batcharr.push({ method: 'post', relative_url: 'me/' + vitNamespace + ':perform', body: "exercice=http://www.vitalclub.net/scripts/getExforFB.php%3Fexid%3D" + exids[i] + "&fb:explicitly_shared=true" });
    FB.api(
        '/',
        'post',
        { batch: batcharr, include_headers: false },
        function(response) {
            ...
        });
}

The error I get (for each exercise) is the following: The action you're trying to publish is invalid because it does not specify any reference objects. At least one of the following properties must be specified: exercise.

I presume this has to do with the way the body in the batch request is formatted but I cannot find the right way to format it. I have tried using encodeURIComponent on the URL representing the exercise but the error is the same.

Anybody has an idea of what the problem is?

Thanks,

Jean

OK, my bad. It was really a stupid error. I had written the parameter "exercise" in French instead of English (so "exercice" instead of "exercise") and that's where the problem was.

I'm also now using $.param to format the parameters so I now have:

var batcharr = [];
var opts;
for (var i = 0; i < exids.length; i++)
{
    opts = { exercise: "http://www.vitalclub.net/scripts/getExforFB.php?exid=" + exids[i], 'fb:explicitly_shared': true };
    batcharr.push({ method: 'post', relative_url: 'me/' + vitNamespace + ':perform', body: $.param(opts) });
}

before calling FB.api and it works like a charm!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM