Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post to Facebook Fan Page Wall from Rails

I made a facebook fanpage for my rails app, and I would like to be able to post data from the rails app to that facebook fanpage's wall.

After a lot of digging around the web, I've found that I am really confused by a lot of the terminology. Most of the solutions I've found, including the fb_graph gem are asking for an ACCESS_TOKEN, which requries an APP_SECRET and an APP_ID. I can find the page's ID but I don't think I can get an APP_SECRET for just a normal fanpage.

The gem doc says I can just use this snippet to post to a feed:

me = FbGraph::User.me(ACCESS_TOKEN)
me.feed!(
    :message => 'Updating via FbGraph',
    :picture => 'https://graph.facebook.com/matake/picture',
    :link => 'http://github.com/nov/fb_graph',
    :name => 'FbGraph',
    :description => 'A Ruby wrapper for Facebook Graph API'
)

Which is great, except that I don't know if I can get an ACCESS_TOKEN without the APP_SECRET and APP_ID.

Do I need to create a Facebook App? (which I believe is different from the normal fan page...) I want users to be able to "like" the page so that things i post in my news feed will show up in their news feed as well.

It's only one page, I don't think I need to authenticate users through OAUTH... I don't want them to post on the page's wall, just the page to post on it's own wall. But I want it to be automated.

How can I do this?

like image 417
rest Avatar asked Apr 01 '11 17:04

rest


4 Answers

Yes, to get an access token, you need client_id and client_secret. Once you got them, this sample app will give you an access token of yourself. (Edit client_id, client_secret and permissions in config/facebook.yml. You'll need "manage_pages" permission at least)

https://github.com/nov/fb_graph_sample

In fb_graph's case, you can post a message as the page itself with this code.

owner = FbGraph::User.me(USER_ACCESS_TOKEN)
pages = owner.accounts
page = pages.detect do |page|
  page.identifier == '117513961602338'
end
page.feed!(:message => 'test')

You can store page.access_token for future use If you already have the page access token, you can simply call like this.

page = FbGraph::Page.new(PAGE_ID, :access_token => PAGE_ACCESS_TOKEN)
page.feed!(:message => 'test')

ps. me.feed!(...) post as the page owner not as the page itself.

like image 180
nov matake Avatar answered Oct 28 '22 11:10

nov matake


The short answer is that yes, you will need a Facebook app. See caveats towards the end of this answer.

All external interactions with Facebook happens via the user of an access token, as you have found. To get an access token for a user, an application must forward the user to a special URL based on your applications App ID and App Secret, which is where they would be presented with the permissions dialog:

Facebook Connect http://s3.amazonaws.com/files.posterous.com/mockupstogo/AIcVc2qmfB9qDwOBuQp4vdylKkM2wZiWhjQezBjTO1L0qym0agkSWQFNNobe/Facebook_Connect.png?AWSAccessKeyId=AKIAJFZAE65UYRT34AOQ&Expires=1301690929&Signature=cwibcaeH6GGbVA3gC2AAyvcEco0%3D

Once the user approves the application, Facebook sends them back to a callback URL with a special code attached. This code can be used to get an access token for the user, which can then perform actions on Facebook as that user (based on the permissions your Facbook application requested).

Since you only want your own account/app to be able to post to the wall, one good idea would be to get a permanent access token and store that token in your app somewhere. While OAuth 2 does support a "user-agent flow" for non-web-applications, Facebook does not support it directly. See the "Desktop Apps" section of https://developers.facebook.com/docs/authentication/ for more.

like image 45
Michelle Tilley Avatar answered Oct 28 '22 12:10

Michelle Tilley


If your application already posts updates to Twitter, you can use Twitter to automatically post those tweets to your Facebook fan page's wall. The following Twitter support article outlines how to link your accounts: https://support.twitter.com/articles/31113

like image 30
Chris Alley Avatar answered Oct 28 '22 11:10

Chris Alley


I really like the gem nov matake has created, and I think I'll switch to using it instead of the others out there. Let me try to expand on that answer a bit.

The facebook documentation is good, but I think it still leaves a lot of holes at the time of writing.

I have found that if you want to make a wall post on a page on behalf of the page itself using an app, that three permissions are required.

  • Request "offline access" permission

https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=YOUR_APP_URL&scope=offline_access&response_type=token

Without offline access, your tokens will expire and your app will have to follow the entire authentication process periodically. Using the url above, you will be provided with a non-expiring token to kick-start the process.

  • Request "publish stream" permission

https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=YOUR_APP_URL&scope=publish_stream&response_type=token

I was unable to post to the page at all without this permission.

  • Request "manage pages" permission

https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=YOUR_APP_URL&scope=manage_pages&response_type=token

Without the manage pages permission, I was able to make wall postings to the page, but they showed up as originating from my personal account and not on behalf of the page itself.

All of the above permissions must be granted to your account manually. After that permissions process, I was then able to use the fb_graph gem and the steps outlined by nov matake using the timeless token produced from the offline_access permissions step.

like image 36
astjohn Avatar answered Oct 28 '22 12:10

astjohn