Skip to content

Uploading Photos and Videos

Benjamin Klotz edited this page Feb 18, 2016 · 3 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 =
# 1.2 and later
@graph =

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

# put_video has the same syntax options available

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, {:caption => "My upload message"}, "me")
@graph.put_picture(params[:file], {:caption => "Message"}, my_album_id)
# videos take title instead of caption params
@graph.put_video(file, content_type, {:title => "My upload message for video"}, "me")

See the Graph API "Photo reference": and "Video reference": 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, { "caption" => "This is the photo caption" }, 123456)

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.