Generic-user-small Michael Madrid 15 posts

I’d like to allow users to upload photos to facebook from my app, but haven’t been able to figure this out. Anything in facebooker to facilitate this? Would be nice to have a section of the book on this.

 
Head_small Mike Mangino 148 posts

I wanted to talk about this, but it is pretty painful. You will need to create a form that posts directly to your server using :canvas=>false. You’ll have to redirect back to the canvas from your controller.

 
Generic-user-small Michael Madrid 15 posts

There seems to be a way to do this w/ RFacebook but I’m not familiar enough to make sense of it. See http://nhw.pl/wp/2008/06/13/uploading-photos-to-facebook-with-rfacebook

I’m gonna look into it more, but would think lots of people (like me :-) would appreciate this as a standard Facebooker feature.

 
Head_small Mike Mangino 148 posts

Oh, do you mean uploading a photo to Facebook? Check out Facebooker::User#upload_photo

It takes a single argument which is a Net::HTTP::MultipartPostFile.

You can create a Net::HTTP::MultipartPostFile by calling new and passing in a filename, a content type and the data to send.

 
Generic-user-small Michael Madrid 15 posts

Yes uploading to Facebook.

I’m trying to do this but having trouble getting the data from the form. My view code is this:

<% facebook_form_for :photo, :url => ‘create’,:html => { :multipart => true } do |form| %> Photo: <%= form.file_field “picture_field”, :class=>”formField” %> <= form.buttons ‘Upload’ %>
<
end %>

All I get back, however, is the param hash:

{“photo”=>{“picture_field”=>”017.JPG”}

Seems the facebook_form_for is different from the form_for. E.g. with form_for you would write :url => {:action=>‘create’} but this doesn’t work with facebook_form_for. Seems file_field tag different too?

 
Head_small Mike Mangino 148 posts

You’ll need to use :canvas=>false in your form url. Facebook won’t pass through multipart form uploads

 
Generic-user-small Michael Madrid 15 posts

Ok so do you mean something like this?

<% facebook_form_for :photo, :url =>photos_url(:canvas=>false), :html => { :multipart => true } do |form| %> Photo: <%= form.file_field “picture_field” %> <= form.buttons ‘Upload’>
<% end %>

I tried this. It goes direct to my tunnlr server as desired, but I still only get back the params:

{“photo”=>{“picture_field”=>”017.JPG”}, “commit”=>”Upload”}

Do I need to use a regular form_for or is there a way to do this w/ the facebook_form_for?

Another note: in addition to using the skip_before_filter :ensure_app… in the controller, unless I also comment out the protect_from_forgery command, I get an ActionController::InvalidAuthenticityToken error.

 
Headshot_small keith Gould 2 posts

Mike, thanks for the above! But a bit confused:

The arguments for MultipartPostFile:
a) is the first argument arbitrary or must it match the filename?
b) what are options for second argument?
c) is the third argument the path to a file? Or is it the object from a File.open() call?

Thank you!

 
Headshot_small keith Gould 2 posts

Mike (and others),

Figured it out:

by “data” they mean:

fullpath = "/path/to/flowers.png" 
file = File.open(fullpath,"rb")
data = ''
file.each_line do |line|
  data += line
end
mpf = Net::HTTP::MultipartPostFile.new("flowers",nil,data)
user.upload_photo(mpf)
file.close
 
Head_small Mike Mangino 148 posts

You can elminate the loop and just use:


  data = file.read

If you’re using attachment fu, we have a backend to store photos on Facebook. If that would be useful, let me know and we’ll post it.

10 posts, 3 voices