hrsetyono
hrsetyono

Reputation: 4464

Rails - Upload picture to Facebook using URL

I'm using Koala for Facebook API and Paperclip for Amazon S3.

I already finished the code for S3 Upload, but having problem for Facebook's upload.

Here's my simplified code:

@user = User.find(params[:id])
@graph = Koala::Facebook::API.new(session[:access_token])
file = open(@user.photo.url(:origin)).read

# ALBUM_ID is constant, its value is the Facebook's album ID
@graph.put_picture(file, { }, ALBUM_ID)

I keep getting this error on last line:

ArgumentError

string contains null byte

I think the way I set file is wrong but I can't find other way to do it.

Thanks before.

Upvotes: 1

Views: 344

Answers (1)

RAJ
RAJ

Reputation: 9747

While uploading picture on facebook using URL, you just need to send picture url directly (you don't need to send binary data).

# REMOVE THIS LINE
file = open(@user.photo.url(:origin)).read

Update API call as:

# ALBUM_ID is constant, its value is the Facebook's album ID
@graph.put_picture(@user.photo.url(:origin), {}, ALBUM_ID)

Some other ways to use put_picture method:

  #     put_picture(file, content_type, {:message => "Message"}, 01234560)
  #     put_picture(params[:file], {:message => "Message"})
  #     # with URLs, there's no optional content type field
  #     put_picture(picture_url, {:message => "Message"}, my_page_id)

Source: Koala gem.

Upvotes: 1

Related Questions