Skip to content

Uploading Photos and Videos

Mauro Bieg edited this page Dec 6, 2017 · 5 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.

Uploading a photo or video

The API class offers the put_picture and put_video method to easily upload media:

@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, {: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":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, { "caption" => "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.

Note that Facebook may limit which albums an app can post to.

IO objects

If you want to upload a StringIO, you'll need to use an adapter that supports uploading IOs:

Faraday.default_adapter = :net_http # or whichever library you want

Uploading Ad Images

For Facebook Ad Images, you unfortunately currently cannot do it by 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))