Uploading photo from server to facebook
I have a facebook canvas application and I want to upload a file from my server to the user's wall.
Facebook says that a form should be created, this is what I did:
<form action="https://graph.facebook.com/me/photos?access_token=<%= @access_token %>" method="post" enctype="multipart/form-data">
<input name="source" type="hidden" value="https://young-water-9853.herokuapp.com/images/1.jpg" />
<input name="commit" type="submit" value="Upload photo" class="cupid-green" />
</form>
This is the error I received:
{ "error": { "message": "(#324) Requires upload file", "type": "OAuthException" } }
How can I make it work?
Solution:
This is the action I am using to post an image to the wall:
get '/post_photo' do
R开发者_StackOverflow社区estClient.post 'https://graph.facebook.com/me/photos', :source => open('http://i52.tinypic.com/313jaxd.jpg'), :access_token => ACCESS_TOKEN
redirect '/'
end
The source parameter needs to be a file object, not a url. If the user was uploading a file from their machine:
<form action="https://graph.facebook.com/me/photos?access_token=<%= @access_token %>" method="post" enctype="multipart/form-data">
<input name="source" type="file" />
<input name="commit" type="submit" value="Upload photo" class="cupid-green" />
</form>
Or if you wanted the user to upload a predefined image, you'd take care of that server-side. The rest_client gem looks like an ideal solution for this:
require 'rest_client'
RestClient.post 'https://graph.facebook.com/me/photos', :source => File.new('/path/to/your/file'), :access_token => YOUR_ACCESS_TOKEN
精彩评论