Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Uploading Photos and Videos

Elliot Crosby-McCullough edited this page · 2 revisions

In order to upload a photo or video to Facebook, your application must have the publish_actions permission; to access the photo after uploading it, you'll also need user_photos or user_videos. See OAuth for more information on how to request extended permissions for your application using Koala.

Uploading a photo or video

The GraphAPI and GraphAndRestAPI classes (and the API class as of 1.2) contain the put_picture and put_video method to easily upload media:

# pre 1.2
@graph = Koala::Facebook::GraphAPI.new(access_token)
# 1.2 and later
@graph = Koala::Facebook::API.new(access_token)

# You can supply several different arguments to put_picture and put_video:
# 1) Path to a photo
@graph.put_picture(path_to_my_file)
# 2) Upload a file directly
@graph.put_picture(file_class_object)
# 3) Rails 3 file uploads (ActionDispatch::Http::UploadedFile)
@graph.put_picture(params[:file])
# 4) Sinatra file uploads (Hash)
@graph.put_picture(file_hash)
# 5) StringIO (1.2beta3)
@graph.put_picture(io, content_type)

# put_video has the same syntax options available
@graph.put_video(path_to_my_video)
@graph.put_video(file_class_object)
@graph.put_video(params[:file])
@graph.put_video(file_hash)

For paths and File objects, you can supply a content type as a second argument.

@graph.put_picture(file_with_weird_extension, content_type)

If a content type is not provided, we'll attempt to detect it automatically using MIME::Types (if available) and or file extension for very common types (if not). In some cases (IO objects, undescriptive filenames), you may be required to explicitly provide the content type yourself.

Additional parameters can be passed after the path/file/etc. and optional content type:

@graph.put_picture(file, content_type, facebook_arguments = {}, user_or_album_id, http_options = {})
# so for instance
@graph.put_picture(file, content_type, {:message => "My upload message"}, "me")
@graph.put_picture(params[:file], {:message => "Message"}, my_album_id)
# videos take title instead of message params
@graph.put_video(file, content_type, {:title => "My upload message for video"}, "me")

See the Graph API "Photo reference":http://developers.facebook.com/docs/reference/api/photo/ and "Video reference":http://developers.facebook.com/docs/reference/api/video/ for all available parameters (if any).

Uploading a photo to a specific album

By default, put_picture will upload the photo to the currenly logged-in user's profile. (Facebook will actually create an album for your application if it has not done so already, and place the photo in there.)

If you have a specific album to which you'd like to upload a photo, you can pass in the album's ID as the third parameter to put_picture:

@graph.put_picture(file, { "message" => "This is the photo caption" }, 123456)
# => { "id" => FACEBOOK_UID_FOR_THE_PHOTO }

This will upload the file to the album with ID 123456. You can also pass in other user's IDs to post to their wall.

IO objects

If you want to upload a StringIO, you'll need to use an adapter that supports uploading IOs. Typhoeus currently does not. Pre-1.2, Koala would automatically switch to the Net::HTTP service for you; with 1.2 and above, you'll need to set the adapter yourself:

Faraday.default_adapter = :net_http # or whichever library you want
Something went wrong with that request. Please try again.