Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem in posting photos in a Batch process. #113

Open
ojha-ravi opened this issue Jan 6, 2016 · 7 comments
Open

Problem in posting photos in a Batch process. #113

ojha-ravi opened this issue Jan 6, 2016 · 7 comments

Comments

@ojha-ravi
Copy link

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
Copy link

dantman commented Jan 6, 2016

  • In that example the attached_files is a reference to the param name of a file that should be POSTed using multipart/form-data with the HTTP request. This only worked in the example because the actual upload example in the docs was a curl command that used -F '[email protected]' -F '[email protected]' to upload two files.
  • The official JS SDK's FB.api has no way to attach a file for upload.
  • Likewise this package has yet to invent and implement an alteration to that interface that would allow for attaching files to upload.
  • Does uploading image is working ? #54 and How to attach a file to Fb.api? #102 are duplicate requests about this.
  • In pull request return request object #50 the author wanted to return the request object so file uploads could be attached. But it was rejected as @prabirshrestha wanted an alternate api pattern for attaching file uploads. Which I agree with, though I might want to review and change the idea for what syntax to use.

@ojha-ravi ojha-ravi changed the title Problem in posting photos a Batch process. Problem in posting photos in a Batch process. Jan 6, 2016
@ojha-ravi
Copy link
Author

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
Copy link

dantman commented Jan 25, 2016

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
Copy link
Author

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
Copy link
Author

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

@dantman
Copy link

dantman commented Jan 26, 2016

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants