How do you get an ID of a friend? facebooker2. rails - ruby-on-rails

I am using facebooker2 and mogli gem to interact with facebook open graph.
My question is how can I get the ID of my friend so I can post him/her sth on his/her wall? All I can do is post to lets say first friend on my list of friends with the following code:
client = current_facebook_client
#user = Mogli::User.find("me", client) unless(client.blank?)
#friends = Mogli::User.find("me/friends", client) unless(client.blank?)
#page = Mogli::Page.find(#friends.first.id)
post = Mogli::Post.new(:message => "Test message at #{Time.now} (sorry, i'll delete it)")
client.post("#{#user.id}/feed", nil, post)
I was playing around with passing it the names of friends and trying some other stuff. I searched google and I also searched through facebook dev forum. But I just dont know how could I do this. How can I get the ID of a friend that I want to post to?
Thank you for any answers!

Trying doing
puts #friends
and see what data log shows. I guess
#friends = Mogli::User.find("me/friends", client) unless(client.blank?)
returns an array of Hashes. And a key for every friend is returned

Related

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.

Rails 3 + Tumblr API: How can I edit multiple posts at the same time with one form?

I'm trying to write a Rails app that fetches a user's Tumblr posts that match certain criteria (for example, only posts that contain certain tags). Once the posts are displayed, I want the user to be able to edit them all at once via one form submit button.
I am authenticating to Tumblr via OmniAuth and making API calls using the tumblr_client gem. See the source code here: https://github.com/monfresh/Fix-my-Tumblr-tags
So far, I've been able to display the correct data, and I am able to edit each post individually. What I don't know how to do is pass in the multiple post IDs as an array, so I can then process them in a loop. Is there a way to do this without creating a new Post model, i.e. without saving the user's posts to my database? Right now, I just have a User model.
I think I figured it out.
In my form_tag, I collect the post IDs in an array via checkboxes:
<%= check_box_tag "post_ids[]", m["id"] %>
and in my controller, I have an action that pulls in those post IDs like so:
def edit_tags
#user = User.find(params[:id])
#posts = params[:post_ids]
end

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.

Retrieve friends locations from Facebook using fb_graph Ruby gem

I'm trying to retrieve the locations of all of a user's friends using the gem: fb_graph. (version 1.7.2)
My permissions are: publish_stream,read_friendlists,offline_access,friends_location,user_location
I've already authenticated the user and stored the offline access token and their facebook id.
user = User.find_by_email("person#email.com")
facebook_user = FbGraph::User.fetch(user.facebook_identifier, :access_token => user.facebook_access_token)
facebook_user.friends.map(&:location) => [nil, nil...]
When I check the permissions from within Facebook it says I have access to friend locations:
http://grab.by/a1Qm
My problem is when I try and get the locations of my friends it always returns nil. I have permissions setup correctly and I know the location should not be nil for everybody.
Does anybody have any suggestions or pointers on how to go about getting friend locations?
Thanks!
So I as able to get the information I needed. You have to call fetch after each call, so
user = User.find_by_email("email#domain.com")
facebook_user = FbGraph::User.fetch(user.facebook_identifier, :access_token => user.facebook_access_token)
friends = facebook_user.fetch.friends
friends.each do |friend|
location = friend.fetch.location
puts location.try(:name)
end
This gets me the data that I want, however it is rather slow iterating over everything and doing an individual call for each piece of data so I refactored to just use straight FQL to return all the data that I needed.
friend_data = FbGraph::Query.new("SELECT uid, first_name, last_name, pic_square, current_location FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1='#{user.facebook_identifier}')").fetch(user.facebook_access_token)
API permissions do not override a user's privacy settings. Is it possible that your facebook user privacy settings block this? Or perhaps all of your friends' privacy settings block this?

Get age of friend on Facebook with Ruby via rFacebook

I am creating a Facebook app and need to access people's ages - only the friends of the user, not the general public.
I am using rFacebook version 0.6.2, setup along the line of this.
I need to get the ages/ birthdays of all of my friends.
As per http://rfacebook.rubyforge.org/ rFacebook isnt being maintained, it suggested Facebooker, but even Facebooker hasn't been updated in months: https://github.com/mmangino/facebooker
I suggest Koala (and not just because I'm an Aussie). Have a read at: https://github.com/arsduo/koala There's also details on setting Koala up on Rails: https://github.com/arsduo/koala/wiki/Koala-on-Rails
I just built a FB app using Koala and a Custom Tab Page last week and it was very quick.
You also need to read: http://developers.facebook.com/docs/authentication/ (pay attention to the mention of scopes and permission levels). As per: http://developers.facebook.com/docs/authentication/permissions/ you must request 'friends_birthday' when you request your scope.
I'm not sure there is an easy way to get all your friends' birthdays in a batch. You might have to traverse each friend and get their info.
As a test, go to: http://developers.facebook.com/docs/reference/api/ and click on the friends link. Then copy the ID of your first friend. In the URL replace '/me/friends' with the ID you copied. Eg: https://graph.facebook.com/me/friends?access_token=ABC123 becomes https://graph.facebook.com/12345678?access_token=ABC123 You will then see the data of that friend, one field of which is birthday.
#i have already asked for user permissions, and have my access token
#https://github.com/arsduo/koala/wiki/OAuth
graph = Koala::Facebook::GraphAPI.new(oauth_access_token)
friends = graph.get_connections("me", "friends")
friends.each do |f|
friend = graph.get_object(f['id'])
puts "#{f['name']} has a birthday on #{friend["birthday"]}"
end
Although, you might be able to use FQL to do a batch.
#FQL taken from http://stackoverflow.com/questions/5063431/easiest-way-to-get-birthday-info-of-all-friends-thorugh-graph-api
fql = "select uid,name,birthday_date from user where uid in (select uid2 from friend where uid1=me())"
#https://github.com/arsduo/koala/wiki/REST-API
#rest = Koala::Facebook::GraphAndRestAPI.new(oauth_access_token)
birthdays = #rest.fql_query(fql)
Good luck!
FQL is probably the way to go here as is mentioned above.
A couple notes:
Not all of your friends will have a birthday accessible (either because they restricted that information, or because they didn't post it). If you only want data for your friends with accessible birthdays, you can add a "and birthday_date" to your where clause.
That query will not return all data, but only the first 100 or so. If you want to get all of them, you will need to request them one page at a time. You can do this by adding a "limit 0,50" clause, to request 50 rows, starting at the 0th one.

Resources