Posting a photo to the Facebook Open Graph from He

2019-08-30 09:01发布

问题:

I am trying to allow a user to share a photo on his Facebook wall. I use Paperclip to store all of my app's photos on S3. So in the below example @photo.image.url is the S3 url of an image that I want to post in some_album. Using the fb_graph gem this is relatively easy on localhost. In my controller I have:

    the_photo = open(@photo.image.url)
    some_album.photo!(
      :access_token => access_token,
      :source => the_photo
    )

the_photo's class on localhost is "Tempfile".

On heroku things get tricky. Using the same code two (seemingly related) things happen. First, the_photo is class type "stringIO". Second when FbGraph posts to Facebook I get the following error:

FbGraph::InvalidRequest (OAuthException :: (#324) Requires upload file):

Heres a few bullet points of facts that might also help:

  • Heroku is a read only system, but apparently you can write to a Tempfile
  • I am on Rails 3.1.0
  • Ruby 1.9.2
  • Heroku Stack Bamboo-Miri

Any help/thoughts/work arounds/questions would be greatly appreciated.

回答1:

I am not sure why this did the trick but this worked for me:

some_album.photo!(
  :access_token => access_token,
  :source => open(@photo.image.url)
)

as opposed to setting a variable photo_response = open(@photo.image.url).