RoR fb_graph application feed - ruby-on-rails

I am using fb_graph gem for Ruby on Rails. I want to post a message on wall but the problem is that when I post the message (feed) it is posted by my user instead of my application, on the application's wall..
so it looks like
Name Surname
message content
via application_name
and I want it to be
application_name
message content
My code is:
application = FbGraph::Application.new(app_id).fetch
application.feed!(:message => 'test', :access_token => access_token)

I have found solution to this question. I have generated access token for application instead of user. This can be found on https://developers.facebook.com/docs/authentication/

Related

Rails post to facebook page

I have a rails app that I wish to be able to post to a fb page. I understand Koala is the gem that facilitates this functionality.
I have created a facebook app and have an app_id & app_secret.
Can my facebook app post to a page or does it need to be done by a page admin?
Application Access Tokens
Facebook applications can also get their own access tokens, which can
be used to manage realtime updates and perform certain other
sessionless activities. To get your app's access token, just run:
#oauth = Koala::Facebook::OAuth.new(app_id, app_secret)
#oauth.get_app_access_token
https://github.com/arsduo/koala/wiki/OAuth
Edit 1
From: https://github.com/arsduo/koala/wiki/Acting-as-a-Page and https://developers.facebook.com/docs/facebook-login/access-tokens/ I understand I will need to obtain an access token and then a page token:
#user_graph = Koala::Facebook::API.new(user_access_token)
pages = #user_graph.get_connections('me', 'accounts')
page_token = #user_graph.get_page_access_token(page_id)
#page_graph = Koala::Facebook::API.new(page_token)
#page_graph.get_object('me') # I'm a page
#page_graph.get_connection('me', 'feed') # the page's wall
#page_graph.put_wall_post('post on page wall') # post as page, requires new publish_pages permission
#page_graph.put_connections(page_id, 'feed', :message => message, :picture => picture_url, :link => link_url)
All of the above is self explanatory however I am stuck on one fundamental being how should my rails app obtain a user_access_token. As I want all of the posting to be performed by my rails app "as a page" I would imaging this user_access_token should be set as a constant once in the rails app and not requested every time the rails app needs to post.
The koala home page states this is possible:
Configuration
Most applications will only use one application configuration. Rather
than having to provide that value every time, you can configure Koala
to use global settings:
# In Rails, you could put this in config/initializers/koala.rb
Koala.configure do |config|
config.access_token = MY_TOKEN
config.app_access_token = MY_APP_ACCESS_TOKEN
config.app_id = MY_APP_ID
config.app_secret = MY_APP_SECRET
# See Koala::Configuration for more options, including details on how to send requests through
# your own proxy servers.
end
https://github.com/arsduo/koala
As I have the app_id and app_secret I am still unsure of how to obtain the access_token and app_access_token required above.
Yes, I am lost.
Edit 2
As the user_access_token expires I am confused as to how this can be hard coded into koala.rb?

Fetching FB pages of a user using Koala

I have an FB account and i have created a fan page for my club and few other pages as well. I have an app in Ruby on Rails. I want to publish some feed on my club fan page. How can i do this?
I have been using Koala Gem and able to successfully post to my wall but not on to the page.
I want to access the list of all the fan pages associated with my account instead of giving the name of specific page.
here is my simple method which i am using to communicate to FB Graph API.
def facebook
#facebook ||= Koala::Facebook::GraphAPI.new(oauth_token)
rescue Koala::Facebook::APIError => e
logger.info e.to_s
nil # or consider a custom null object
end
Answer submitted by Sumit can be an approach but after searching around some more forums, finally i got an elegant way to do it.
#user_graph = Koala::Facebook::API.new(user_access_token)
pages = #user_graph.get_connections('me', 'accounts')
# get access token for first page
first_page_token = pages.first['access_token']
# or: retrieve access_token for a given page_id
page_token = #user_graph.get_page_access_token(page_id)
Passing on the "accounts" parameter to get_connection worked elegantly for me.
Here is the reference to API.
And one last thing, never forget to add the "manage_pages" permission in your permissions list.

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?
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")
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.
I highly highly recommend the new gem Koala for working with facebook.
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.

How to pass an access token with KOALA gem for facebook real time updates for feeds and posts

I am trying to develop a facebook integration to fetch the wall posts using KOALA Gem (1.1.0), and Rails 2.3.8 . I can easily generate user_access_token and page_access_token and able to get data using graph API. But when I am using realtime update I can't get datas which need access token. I can access all public information with KOALA api.
I guess we need to pass access_token(user_access_token or page_access_token). I couldn't find an option to pass access token. I checked the RealTimeUpdate class and found only two arguments app_access_token and app_id, which may not be sufficient to get the protected data like feeds and post
Here am attaching the sample code:
#updates = Koala::Facebook::RealtimeUpdates.new(:app_id => YOUR_APP_ID, :secret => YOUR_APP_SECRET )
=> Koala::Facebook::RealtimeUpdates:0x10331fb88 #graph_api=#, #secret=”81297xxxxxxxxxxx”, #app_access_token=”1779yyyyyyy|xxxxxxx”, #app_id=”1779yyyyy”
and you can see app_access_token and access_token has been set the same.
Then I tried to retrieve the access token as follows but it returns nil.
>> #updates.access_token
=> nil
Kindly advise how do I go forward?
Seems this is a bug in facebook . Please have a look #
http://bugs.developers.facebook.net/show_bug.cgi?id=18048

How to post on my friend's Facebook Wall using koala gem?

I am trying to post a message on my friend's Facebook wall using Koala Gem in my Web Application.
I am trying using the following code
#user.put_wall_post("Hey, Welcome to the Web Application!!!!",{:name => "Friend's Name"} )
I have also tried replacing the name of my friend with his Facebook Id, but it is of no help...
#user.put_wall_post("Hey, Welcome to the Web Application!!!!",{:name => "10001010101010"} )
But, both the above methods post the message on my wall. What am I wrong with??
The name symbol doesn't represent the name of the user on Facebook but the title of the message! Try this instead:
#user.put_wall_post("Hey, Welcome to the Web Application!!!!", {:name => "..."}, "id_of_your_friend")
Tell me if it works.

Resources