rugbert
rugbert

Reputation: 12653

Using fb_graph to post to Facebook fan page from Rails 3

I hear that fb_graph is the way to go and I already have my app registered with Facebook but I don't know how to get the access token to post things. I have my app ID and secret but I need to get that access token. All I'm trying to do is post to a Facebook fan page (as the page).

How do I get the access token?

Upvotes: 4

Views: 1709

Answers (4)

Stuart hanscombe
Stuart hanscombe

Reputation: 41

Get the user:

user = FbGraph::User.me(access_token)
user.fetch

To see the users accounts details:

user.accounts

Select the Facebook page that you want to post to:

account = user.accounts.select {|account| account if account.name == "*Your Page Name*"}.first

(account.access_token => the pages access token) 
(account.identifier => page id)

Create new page instance:

page = FbGraph::Page.new(account.identifier)

Post to the page:

note = page.note!(:access_token => account.access_token, :subject => "Hello World", :message => "hey, this is a test from rails")

Upvotes: 4

Stephen Atty
Stephen Atty

Reputation: 336

If you do a call to https://graph.facebook.com/me/accounts

and pass in your access_token it will return data listing all the pages you have access to and the access_tokens needed to post to those walls.#

You get your access token by logging in and the access token is passed back by the login process.

Upvotes: 0

CamelCamelCamel
CamelCamelCamel

Reputation: 5200

I highly highly recommend the new gem Koala for working with facebook.

Upvotes: 1

Alvin K.
Alvin K.

Reputation: 4379

The step-by-step procedure for access token with Oauth 2.0 is found here: http://developers.facebook.com/docs/authentication/. If you require a sample code in Ruby, create a new app and under "Hosting URL", click on "get one" and select Ruby programming language when prompted. It also contains code to pull from Graph API.

Upvotes: 1

Related Questions