Reputation: 4697
I am doing a rails project that involved using Koala gem to call facebook graph API. Is it possible to post to facebook wall with picture/video attachment (not picture link) via graph API?
I'm able to to put picture using this command
graph = Koala::Facebook::API.new(access_token)
graph.put_picture(params["picture_path"]) #where params["picture_path] is ActionDispatch::Http::UploadedFile object
but this only upload to album
I failed doing this:
graph = Koala::Facebook::API.new(access_token)
graph.put_wall_post("hello", {"picture" => params["picture_path"]} ) #where params["picture_path] is ActionDispatch::Http::UploadedFile object
Error:
undefined method `local_path' for #<ActionDispatch::Http::UploadedFile:0x00000106100a70>
Help ?
Upvotes: 2
Views: 4724
Reputation: 3282
This worked for me:
graph.put_picture(params["picture_path"], {:message => "Message"})
(Taken from: https://github.com/arsduo/koala/wiki/Uploading-Photos-and-Videos)
Upvotes: 0
Reputation: 224
Here an example with an image generated with rmagick:
First create an image:
@clown = Magick::ImageList.new("public/images/framed_clown.jpg")
Then put in an album (you must use StringIO) and to_blob:
@clown_id = StringIO.open(@clown.to_blob) do |strio|
response = @graph.put_picture(strio, "image/jpeg")
response['id']
end
Now @clown_id contains the ID of the image, to get the URL:
@picture_url = @graph.get_picture(@clown_id)
Finally we can post to the wall? Well, remember that you need publish_stream permissions:
FACEBOOK_SCOPE = 'user_likes,user_photos,user_photo_video_tags,publish_stream'
So we can say:
begin
@graph.put_wall_post("This a test", {"picture" => @picture_url})
rescue => e
if(e.fb_error_type == "OAuthException")
# Already Posted
end
end
Because is better to put a control for duplicated status message....
Enjoy!!!!!
Upvotes: 1