Uploading a file to Facebook using Koala on Ruby on Rails

I followed the next blogpost to find out how to create Facebook events remotely using my application. However, I had problems downloading images from my application, because I do not have images stored locally in my application, they are stored in AWS.

@graph = Koala::Facebook::GraphAPI.new(@token) picture = Koala::UploadableIO.new(@event.photo.url(:small)) params = { :picture => picture, :name => 'Event name', :description => 'Event descriptio :start_time => datetime, } 

is the following code that I use to send photos to Facebook when Facebook events are generated in my application. The problem is that Rails throws an error: there is no such file or directory - http://s3.amazonaws.com/ColumbiaEventsApp/photos/21/small.jpeg?1312521889 .

Does anyone who knows more about Rails development know if there is a way to treat the URL as a file path? The UploadableIO class expects a file path, and I'm struggling to figure out if there is a way in Ruby to handle URLs such as file paths. The way to upload photos to the application is as follows:

picture = Koala :: UploadableIO.new (File.open ("PATH TO YOUR EVAGE IMAGE"))

if that helps.

I appreciate any new understanding of this issue.

+4
source share
4 answers

With Koala 1.2.1, this is a very elegant solution. Here is an example code to create an album and download it from a remote AWS link (by the way, it took about 30 lines in PHP with the PHP SDK!

 @foo = Foo.find(params[:foo_id]) albuminfo = @graph.put_object('me','albums', :name=>@foo.title) album_id = albuminfo["id"] @graph.put_picture(@foo.remote_image_path,{}, album_id) 
+4
source

Ok, so I played around and figured out how to post photos.

I mainly used the open-uri library to convert image links to file objects, which can then be uploaded to UploadableIO and uploaded to Facebook. This is the code that worked:

 require 'open-uri' OpenURI::Buffer.send :remove_const, 'StringMax' if OpenURI::Buffer.const_defined?('StringMax') OpenURI::Buffer.const_set 'StringMax', 0 picture = Koala::UploadableIO.new(open(@event.photo.url(:small)).path, 'image') params = { picture: picture, name: @event.name, description: @event.description, location: @event.location, start_time: datetime } @graph.put_object('me', 'events', params ) 

The OpenURI StringMax constant needed to be changed, because the image files I used were small enough for the files to be processed as strings, not file objects.

Hope this helps anyone trying to fix it!

+9
source

Facebook recently posted an update that allows you to post images using public URLs ( http://developers.facebook.com/blog/post/526/ ). The Koala library that you use supports ( https://github.com/arsduo/koala/blob/master/lib/koala/graph_api.rb#L102 ) so you can post photos hosted on S3 without using OpenURI: : Buffer.

+1
source

For Facebook Pictures ads , unfortunately, you cannot currently do this at the URL, thus:

 require 'open-uri' img_data = open(my_post.image.url :medium).read img = graph.put_connections('act_X', 'adimages', bytes: Base64.encode64(img_data)) 
+1
source

All Articles