Using fb_graph to post to Facebook fan page from Rails 3 - ruby-on-rails

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.

Related

Where I can take correct API token for requests to Trello behalf on user (ruby-trello)

I am using Ruby on Rails 5.2 and gems: ruby-trello, devise, omniauth-trello.
I want to make authorized requests on behalf of Trello user same as shows here: https://github.com/jeremytregunna/ruby-trello#multiple-users
Example from git docs:
#client_bob = Trello::Client.new(
:consumer_key => YOUR_CONSUMER_KEY,
:consumer_secret => YOUR_CONSUMER_SECRET,
:oauth_token => "Bob's access token",
:oauth_token_secret => "Bob's access secret"
)
My steps:
User (Bob) sign in with Trello and get his own oauth_secret and oauth_token
App creates a Trello::Client for Bob using:
his own oauth data (:oauth_token, :oauth_token_secret)
I got consumer_key from here: https://trello.com/app-key (in the top of page, first block with key field)
consumer_secret was taken from https://trello.com/app-key too, but from the bottom of page, last block with key secret
After this, I'm trying to get any data from Bob's trello account (boards, lists, tasks etc.) but always getting 500 error (invalid token).
Could you explain what I'm doing wrong?
Thank you in advance.
What I did was implementing a session controller to request and authorize access to user's trello and then call Trello::Client with the authorization params inside the callback method on the controller.
Check out this:Trello OAuth 1.0 authorization with Ruby
Then inside the authorization method you can call Trello::Client using :oauth_token and :oauth_token_secret from get_access_token call or store them both on the session object and use them anywhere.

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?

What is Facebook callback_url and how to use it in rails?

I am using the Facebook Graph API in my rails projects, no matter I use oauth2 gem or koala, It need callback_url
Oauth2
token = client.auth_code.get_token('code_value', :redirect_uri => 'http://localhost:8080/oauth/callback')
Koala
#oauth = Koala::Facebook::OAuth.new(app_id, app_secret, callback_url)
I try to use http://localhost:3000/callback in my project, but it's not working.
Should I develop a routes for that?
like: get 'callback' => 'oauth#callback'?
What should I write in the callback method in OauthController, what does it use for? Thanks
Yes, you should.
Basically, OAuth uses callback data to provide tokens for authenticating users.
For example
user clicks on "sign in" (or whatever) link and your app redirects they to the OAuth provider (or open it in the iframe).
user permits to your app to use they profile details
OAuth provider send callback to your app with unique code
app uses that code to get secure access token for API communications
That's just a basic example.
In your case you need to implement controller that will parse callback data.
Here is the code example
#oauth = Koala::Facebook::OAuth.new(api_key, app_secret, callback_url)
=> #<Koala::Facebook::OAuth:0x007fc919d014e0 #app_id=1234567890, #app_secret="FaKeAppSecretKey", #oauth_callback_url="http://localhost:3000/callback">
#oauth.url_for_oauth_code
=> "https://www.facebook.com/dialog/oauth?client_id=893637180663238&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback"
And when you go to https://www.facebook.com/dialog/oauth?client_id=893637180663238&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback FB will redirect you to
http://localhost:3000/callback?code=CODE_FROM_CALLBACK
Then you should use implement controller that uses code to get access token
access_token = #oauth.get_access_token(params[:code])
=> "ACCESS_TOKEN"
#graph = Koala::Facebook::API.new(access_token)
=> #<Koala::Facebook::API:0x007fc91a903ae0 #access_token="ACCESS_TOKEN", #app_secret=nil>
profile = #graph.get_object("me")
=> {"id"=>"4492344324865", "email"=>"my_fake_email_address#gmail.com", "first_name"=>"Roman", "gender"=>"male", "last_name"=>"Sotnikov", "link"=>"https://www.facebook.com/app_scoped_user_id/4492344324865/", "locale"=>"en_US", "name"=>"Roman Sotnikov", "timezone"=>6, "updated_time"=>"2015-05-18T05:19:54+0000", "verified"=>true}
Please check https://github.com/arsduo/koala/wiki/OAuth for additional info.
Callback Url is yours applications url -- a GET route -- you want the third party application to redirect to, after its done its work.
So in your routes.rb file simply create a get route
get 'facebook_graph_callback', to: 'controller_name#action'
#A get route which is connected to a controller action
Usually the third party will give you some sort of information back. Quite often its some sort of code. In your controller action you can use find them in params hash.

Koala - Create tab in facebook (rails)

I want to create tabs on FB.
I have this code:
#graph = Koala::Facebook::API.new(Client.find(session[:id_client]).oauth_token)
#graph.put_connections("4154829881847172/tabs","POST", {:access_token => "AAABwdtYjsyoBAMcg558a4FYnZBkZBUiPKmcWWssssWoNZC2yjqE43ghoR9uTiFIhT3ErkQYx45RrrPeOD0ZCvFgnmRIUh9lqRUw5KIlWwxtRW3GvbIEUWp0yB2", :app_id => '1236553701115690'})
But I have a problem
-> OAuthException: (#210) Subject must be a page.
Is 4154829881847172 the correct ID for the page you're trying to add the tab to?
And are you definitely using the page's access token? (as opposed to a user access token for the page admin)
Either of those being incorrect means you're passing a something-other-than-a-page's ID in place of the Page ID
This did not work for me as well. And I think the reason is http://developers.facebook.com/bugs/194192344040832/. Running this on over Graph API Explorer gives the same result.
But I did using the following way
#graph = Koala::Facebook::GraphAPI.new("user_access_token")
#newgraph=Koala::Facebook::GraphAPI.new("page_access_token")
#newgraph.put_connections("me","tabs", {:app_id => 'your_app_id'})
You basically make a graph object with the page access token and then run the put_connections method.

RoR fb_graph application feed

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/

Resources