I would like to post a new photo to a user using `Net::HTTP::multipart`
from a Heroku application to Facebook.
I have the following JSON object:
{"message"=>"My message",
"image"=>#<ActionDispatch::Http::UploadedFile:0x00000004242490
@original_filename="neEZYMAnBI.jpg",
@content_type="application/octet-stream", @headers="Content-Disposition:
form-data; name=\"image\";
filename=\"/home/user/public/direct/fb_images/neEZYMAnBI.jpg\"\r\nContent-Type:
application/octet-stream\r\n",
@tempfile=#<File:/app/tmp/RackMultipart20110818-1-18qnwtj>>,
"method"=>"post", "access_token"=>"my_access_token", "format"=>"json"}
I tried to use:
require 'net/http/post/multipart'
url =
URI.parse('https://graph.facebook.com/me/photos?access_token=...)
File.open(params[@tempfile]) do |jpg|
req = Net::HTTP::Post::Multipart.new url.path,
"file" => UploadIO.new(jpg, "image/jpeg", "image.jpg")
res = Net::HTTP.start(url.host, url.port) do |http|
http.request(req)
end
end
But:
1. It is not sending the data.
2. I cannot find how to send the params[:message] with the file in
order to have a caption for the image.
Can someone suggest a solution?
Thanks
on 2011-08-19 14:57
on 2011-08-19 14:57
I also found: http://forrst.com/posts/post_a_photo_to_the_fb_gra... class FbPhotoUploader require 'net/http/post/multipart' # multipart-post gem require 'mime/types' #mime-types gem require 'net/https' def upload(path, access_token, options={}) photo = File.open(path) params = {:access_token => access_token} params.merge!(options) url = URI.parse("https://graph.facebook.com/me/photos") req = Net::HTTP::Post::Multipart.new "#{url.path}?#{params.to_query}", "file" => UploadIO.new(photo, mime_for_file(photo), photo.path) n = Net::HTTP.new(url.host, url.port) n.use_ssl = true n.verify_mode = OpenSSL::SSL::VERIFY_NONE n.start do |http| http.request(req) end end private def mime_for_file(f) m = MIME::Types.type_for(f.path.split('').last) m.empty? ? "application/octet-stream" : m.to_s end end Can someone help to adjust that at least?
on 2011-08-19 15:00
I also found: http://forrst.com/posts/post_a_photo_to_the_fb_gra... class FbPhotoUploader require 'net/http/post/multipart' # multipart-post gem require 'mime/types' #mime-types gem require 'net/https' def upload(path, access_token, options={}) photo = File.open(path) params = {:access_token => access_token} params.merge!(options) url = URI.parse("https://graph.facebook.com/me/photos") req = Net::HTTP::Post::Multipart.new "#{url.path}?#{params.to_query}", "file" => UploadIO.new(photo, mime_for_file(photo), photo.path) n = Net::HTTP.new(url.host, url.port) n.use_ssl = true n.verify_mode = OpenSSL::SSL::VERIFY_NONE n.start do |http| http.request(req) end end private def mime_for_file(f) m = MIME::Types.type_for(f.path.split('').last) m.empty? ? "application/octet-stream" : m.to_s end end Can someone help to adjust that at least?
on 2011-09-14 15:42
Hi, were you able to come up with a solution? I have the same problem myself Thanks
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.