So here is what I'm trying to do. I'm trying to use the facebook batch api to post a single photo to multiple facebook pages at the same time. I first put everything into an array (fb_batch) then encode it using JSON. This method works just fine for posting messages and links. In the example below the @pages array is an array of facebook pages that contains the facebook page information including a valid access token. I know the access token is valid because elsewhere in the program I can post an individual photo using this same access token.
fb_batch = []
top_access_token = nil
@pages.each do |page|
top_access_token = page.access_token if top_access_token.nil?
if image.nil?
fb_batch << {method: 'POST', relative_url: 'me/feed?access_token=' + page.access_token, body: body}
else
fb_batch << {method: 'POST', relative_url: 'me/photos?access_token=' + page.access_token, body: body, attached_files: 'file1'}
end
end
c = Curl::Easy.new('https://graph.facebook.com')
c.multipart_form_post = true
c.verbose = true
post_data = [Curl::PostField.content('access_token', top_access_token), Curl::PostField.content('batch', fb_batch.to_json)]
post_data << Curl::PostField.file('file1', image.image.current_path) if !image.nil?
c.http_post(post_data)
Notice how I only add the file if there is an image. So this same code works for when I'm posting messages or links to multiple accounts.
The error comes when I post an image to multiple accounts. It will apply the image to the first account an the fb_batch array, but all the other accounts have an error. Do I have to attach the image separately for each account I wish to post the photo to each image having a different attached_file name? To me this means that the request would be huge since I would potentially have to attach the same image 50 times if I was posting to 50 accounts.
Here is the response I get from facebook:
[{"code":200,"headers":[{"name":"Access-Control-Allow-Origin","value":"*"},{"name":"Cache-Control","value":"private, no-cache, no-store, must-revalidate"},{"name":"Connection","value":"close"},{"name":"Content-Type","value":"text/javascript; charset=UTF-8"},{"name":"Expires","value":"Sat, 01 Jan 2000 00:00:00 GMT"},{"name":"Pragma","value":"no-cache"}],"body":"{\"id\":\"416193168426655\",\"post_id\":\"159594100753231_416193185093320\"}"},{"code":400,"headers":[{"name":"Access-Control-Allow-Origin","value":"*"},{"name":"Cache-Control","value":"no-store"},{"name":"Connection","value":"close"},{"name":"Content-Type","value":"text/javascript; charset=UTF-8"},{"name":"Expires","value":"Sat, 01 Jan 2000 00:00:00 GMT"},{"name":"Pragma","value":"no-cache"},{"name":"WWW-Authenticate","value":"OAuth \"Facebook Platform\" \"invalid_request\" \"(#1) An unknown error occurred\""}],"body":"{\"error\":{\"message\":\"(#1) An unknown error occurred\",\"type\":\"OAuthException\",\"code\":1}}"}]
Sorry it is messy. You can see that the first code is 200 which posts the image successfully and returns the image ID. The second code is a 400 saying An unknown error occurred. I assume this means that it couldn't find the image or something of that sort. If I switch the two accounts around in the array then it will post successfully to the other account and will fail on the account that just succeeded.
Thanks for reading all the way to the end! Here is your pot of gold...
My answer somewhat skates around debugging your problem and offers a possible alternative. Back last year, Facebook launched the ability to upload photos by supplying a source URL instead of having to supply multipart data. Therefore, I recommend the following workaround:
This will post the photo directly to the Page's Timeline. Batching that call should not make any difference. Hope this helps!