Thuzi / facebook-node-sdk

Modeled from the (Facebook Javascript SDK), now with the facebook-node-sdk you can now easily write the same code and share between your server (nodejs) and the client (Facebook Javascript SDK).
Other
736 stars 250 forks source link

Problem in posting photos in a Batch process. #113

Open ojha-ravi opened 8 years ago

ojha-ravi commented 8 years ago

Hey Guys, i have been trying to post local photo(s) on fb with node terminal app in batch process with the following,

FB.api('', 'post', {
    batch: [{
        method: "POST",
        relative_url: "{Object-id}/feed",
        body: "message=post one",
        attached_files: file1
    }, {
        method: "POST",
        relative_url: "{Object-id}/feed",
        body: "message=post two",
        attached_files: file2
    }]
}, function(res) {

    if (!res || res.error) {
        console.log(!res ? 'error occurred' : res.error);
        return;
    }

    console.log(res[0]);
    console.log(res[1]);
});

The error i am getting is following,

{ code: 400, headers: [ { name: 'WWW-Authenticate', value: 'OAuth "Facebook Platform" "invalid_request" "File type has not been attached"' }, { name: 'HTTP/1.1', value: '400 Bad Request' }, { name: 'Cache-Control', value: 'no-store' }, { name: 'Content-Type', value: 'text/javascript; charset=UTF-8' } ], body: '{"error":{"message":"File type has not been attached","type":"GraphBatchException","fbtrace_id":"FvPXVmPSTNN"}}' } { code: 400, headers: [ { name: 'WWW-Authenticate', value: 'OAuth "Facebook Platform" "invalid_request" "File type has not been attached"' }, { name: 'HTTP/1.1', value: '400 Bad Request' }, { name: 'Cache-Control', value: 'no-store' }, { name: 'Content-Type', value: 'text/javascript; charset=UTF-8' } ], body: '{"error":{"message":"File type has not been attached","type":"GraphBatchException","fbtrace_id":"FvPXVmPSTNN"}}' }

This is similar to the example given in fb docs titled under Uploading binary data. I think the title says that you need to upload binary files, which i don't know how to that, Any help?

If i remove the attached_files from the code above it is able to create two post on fb on that node

Even if i replace {Object-id}/feed with {Object-id}/photos it's throwing the same error.

If some has done this before can you please post the snapshot of your code. (Thanks) Please help me with this. Any help will really appreciated.

Similar Post

dantman commented 8 years ago
ojha-ravi commented 8 years ago

Hi @dantman , thanks for your quick reply... I have tried posting with form-data to actually insert image in HTTP POST request which worked so i was little excited that this may also be possible.

var form = new FormData(); //Create multipart form
form.append('file', fs.createReadStream(picPaths[0])); //Put file
form.append('message', "Hello"); //Put message
var ACCESS_TOKEN = "ACCESS_TOKEN";
var options = {
    method: 'post',
    host: 'graph.facebook.com',
    path:  '{Object-ID}/photos' + '?access_token=' + ACCESS_TOKEN,
    headers: form.getHeaders(),
}

var request = https.request(options, function(res) {
    console.log(res, false, null);
});

form.pipe(request);

request.on('error', function(error) {
    console.log(error);
});

Glad that you are thinking about this. Thanks again for your very detailed info.

dantman commented 8 years ago

Would you care to test out 1.1.0-alpha1 (use npm install fb@^1.1.0-alpha1).

I've added upload support. You can pass a Buffer or a ReadableStream as value or an object containing { value: ..., options: { ... } } where value is a Buffer or a ReadableStream and options can contain contentType, filename, and any other options available in formData/form-data. If you that for any of your params multipart/form-data will be used to upload.

FB.api('', 'post', {
    batch: [{
        method: "POST",
        relative_url: "{Object-id}/feed",
        body: "message=post one",
        attached_files: 'file1'
    }, {
        method: "POST",
        relative_url: "{Object-id}/feed",
        body: "message=post two",
        attached_files: 'file2'
    }],
    file1: fs.createReadStream('post1.jpg'),
    file2: fs.createReadStream('post2.jpg')
}, function(res) {

    if (!res || res.error) {
        console.log(!res ? 'error occurred' : res.error);
        return;
    }

    console.log(res[0]);
    console.log(res[1]);
});
ojha-ravi commented 8 years ago

Hi @dantman , sry for late reply, i tried to follow steps that you suggested.

I am able to post two (or more) posts, but pic is not getting attached to the post.

Please suggest if i am missing something obvious. Thanks,

ojha-ravi commented 8 years ago

Hi @dantman , it works only if i change ...{Object-id}/feed... to {Object-id}/photos

dantman commented 8 years ago

You should ask on SO what the proper graph API call is to publish a post with a photo attachment.

I don't see any documentation saying posting to */feed with an upload is the way to do that.

ojha-ravi commented 8 years ago

Hi @dantman, i was just able to post with multiple photos with this library.

You can find the details here.

Thanks Again for your help.