Fetching FB pages of a user using Koala - ruby-on-rails

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.

Related

Update Twitter image and name after every login - Rails

My users can login only by Twitter and I'm using the omniauth-twitter gem (https://github.com/arunagw/omniauth-twitter).
Rails 4.1.2 and Ruby 2.1.0
It works perfectly, the only problem is that whenever a user changes his profile photo or name, the database is not updated with the changes.
How can I make my app to see after every login if any of the attributes is changed and if so, to update the database with them?
Thank you very much.
You should be able to add some code to your twitter callback action -- the action that is called by the path /auth/twitter/callback -- to read from the OmniAuth::AuthHash that is returned to you via request.env['omniauth.auth']. Basically, omniauth-twitter packages up the OAuth Provider's response into a convenient objet and places it in the request hash. So... something like this:
def twitter
# ...
update_picture
end
def update_picture
auth = request.env['omniauth.auth']
current_user.picture = auth.info.image
# or whatever
end
FYI: The OmniAuth::AuthHash object is basically a hash with benefits. It is an instance of a Hashie object, in case you're interested in learning more. (Off the top of my head, I think it's a Mash hash.)
Also, you can find and example return response here, in the omniauth-twitter readme.

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.

Rails 3 Linkedin API gem

I'm trying to set up the linkedin api in a rails 3 app using the linkedin gem. I don't want the user to have to authenticate my app in order for the API to get their info. I only need one piece of their public profile (the headline). So, maybe I should just be using xml or json to pull this off (not exactly sure how to get that with linkedin either).
I have the following in a helper so that I can call linkedin_header() in a loop of users. I only have 'client' as the last line of the following code while debugging. It outputs as expected (#). It seems like I am only a step away from success. How can I access a given users headline? I have tried using "client = client.profile(:url => 'linkedin_user_url')", but that return "Call must be made on behalf of a member".
def linkedin_header(account_user)
user = User.find(account_user)
account = Account.where(:user_id => user, :external_id => 1)
api_key = 'aaaaaaaa'
api_secret = 'bbbbbbbb'
client = LinkedIn::Client.new(api_key, api_secret)
rtoken = client.request_token.token # this returns correctly
rsecret = client.request_token.secret # this returns correctly
client
# client = client.profile(:url => 'linkedin_user_url')
end
So, I guess I have two questions. Is my request (public headline of any user) too simple for the above...should I be using XML or JSON. And, if Im close...can I make the API work for me without the user having to authenticate via linkedin.
Based off of what I read from the LinkedIn API reference (http://developer.linkedin.com/documents/authentication)
You have to make requests to their API only after being authenticated. (Using OAuth Keys) Rather than just grabbing the publicly available information.
It seems like since you want a small piece of information (the public headline of any user) you'd want some sort of implementation like Facebook's OpenGraph. After looking around on LinkedIn, I don't see any sort of public implementation like that.
I would suggest checking out this gem:
https://github.com/yatishmehta27/linkedin-scraper
It seems to be the type of solution you're looking for.

How would I retrieve the latest 'x' number of posts from multiple Facebook users with the Koala gem?

I'm looking to be able to return a JSON list of posts from a set of Facebook users with the Koala gem either through FQL or the Graph API. Currently I'm using the batch function of Koala to make a call for the first 'x' number of items in from each user feed and then flattening the array and sorting by time. The issue with this is that I would like to be able to get the first 'x' number of items where it doesn't get 10 from each user but simply the 10 most recent posts from the users I queried so I can retrieve the next 10 from the feed akin to what you would expect from your own home feed.
This is what I currently have:
#oauth = Koala::Facebook::OAuth.new
#access_token = #oauth.get_app_access_token
#graph = Koala::Facebook::API.new(#access_token)
artists = []
followings.each do |following|
artists << following.artist
end
feed = #graph.batch do |batch_api|
artists.each do |artist|
feed | batch_api.get_connections(artist.facebook, "feed", {"limit" => "10"})
end
end
feed.flatten!
feed.sort! { |x, y| y["created_time"] <=> x["created_time"] }
EDIT
I believe I've found one possible solution with FQL as follows
batch_api.fql_query("SELECT post_id, source_id, likes, actor_id, message, type FROM stream WHERE source_id IN(artist1, artist2...)")
But I am now returned the error
[#<Koala::Facebook::APIError: OAuthException: (#606) Queries for multiple source_ids require a non-zero viewer that has granted read_stream permission>]
The issue with this is that my application does not require someone to login with Facebook and so I cannot request a read_stream permission. Is there anyway the Facebook app itself can request this?
It should work, but there is a FB bug described here:
I think that there is nothing to do on your part to solve it.

Session problem using Facebooker with Ruby on Rails

I am reading the book Facebook Platform Development in order to try to code a small game for Facebook, and I have come across a "little" problem: I am trying to insert a user every time this is logged, into a database in my computer. I am using a couple of methods written in the book, but there seems to be a couple of problems:
I can't seem to retrieve the session_key from Facebook using the Facebooker helpers for RoR, and thus this value is null into the table in my database.
Every time I reload the webpage, I can see that even though the facebook_id is the same, the same user is added in another row to my table in the database, even though it shouldn't; it's just supposed to update the attribute session_key if this changes -anyway, right now this is null.
These are the three methods I am using in order to perform all this:
def self.for(facebook_id,facebook_session=nil)
user = User.find_or_create_by_facebook_id(facebook_id)
unless facebook_session.nil?
user.store_session(facebook_session.session_key)
end
end
def store_session(session_key)
if self.session_key != session_key
update_attribute(:session_key, session_key)
end
end
# Re-create a Facebooker::Session object outside a request
def facebook_session
#facebook_session ||= returning Facebooker::Session.create do |session|
# Facebook sessions are good for only one hour storing
session.secure_with!(session_key,facebook_id,1.hour.from_now)
end
end
Thanks a lot in advance to everybody!1.
Hey sadly facebook changes its API all the time!
Make sure that the book is up to date and that none of the API has changed as of when the book was written. Also check that the gem is also up to date.
I personally use http://github.com/chrisdinn/hyper-graph when dealing with facebook. It makes calls to the facebook graph (graph.facebook.com)

Resources