I am trying to build an application with Ruby on Rails that gets the most recent Facebook posts from a public page and displays them in a certain way.
Currently, I am planning to get the posts using the Koala gem and Graph API’s get_connection
client = Koala::Facebook::API.new(oauth_token)
client.get_connection('someuser', 'posts',
{limit: @options[:max_items],
fields: ['message', 'id', 'from', 'type',
'picture', 'link', 'created_time', 'updated_time'
]})
However, I believe this would require me to update the feed manually be re-running this every X hours. It would be much more convenient if it was possible to setup a webhook that listens for a new post and updates it accordingly. I found the link below, but can’t quite make sense of it/whether this possible or if there is a better way than I am currently doing it.
https://developers.facebook.com/docs/graph-api/webhooks
If it is possible, how might I achieve it, or is there another better way?
2
Answers
I believe this is the solution but never tried it, you might correct me if I’m wrong:
feed
(or other things).https://graph.facebook.com/v2.6/me/subscribed_apps?access_token=PAGE_ACCESS_TOKEN
. The access token must have enough permissions for that (eg:manage_pages
).Hope it helps!
There is two steps to achieve it, first you need suscribe your app to the webhook for pages you can do it with the Facebook app panel control or using Koala Real-time Updates (webhook references), then you need to add your app to the subscribed apps of the page. Here is the code that I use for these steps:
#subscribe app
@updates = Koala::Facebook::RealtimeUpdates.new(app_id:
facebook_app_id,secret: facebook_app_secret)
@updates.subscribe("page", "feed", callback_url, verify_token)
#subscribe page
api = Koala::Facebook::API.new(page["access_token"])
api.graph_call("#{page["id"]}/subscribed_apps",{},"post",{})
If you don’t know the page access_token see the
Koala wiki
Important: You need use the protocol “https” for the callback_url and the verify_token size can’t be longer than 64