Issue retrieving friendlists from Facebook through Koala gem - facebook

I am working with a project started by someone else which interacts with Facebook through the Koala gem. For some odd reason it will allow me to create friend lists but when I retrieve them I get nothing. I can verify I have friend lists available through the Graph API by using Facebook's Graph API Explorer tool, and I do have the "manage_friendlists" permission in the application. I can also verify that it successfully adds friend lists through this application and trying to add an identically named friend list will return an error (which it should). However, when it gets to the point of actually retrieving them through the app, it simply returns the hash
{:me => {:id => 'my_id'}}
with nothing else. The method is written as
def friendlists
graph_api.get_objects('me', {fields: 'friendlists'})['me']['friendlists']['data']
end
which gives an 'undefined method [] for nilClass' since there is no 'friendlists' nested hash returning. I am using Koala 1.4.0 if that makes any difference. Any thoughts or suggestions? Thanks!

I am also using koala gem... you can use it as follows:
session['fb_access_token'] is the one when user is logging in, I am assigning as follow:
session['fb_access_token'] = auth['credentials']['token']
def self.fetching_facebook_friends
#graph = Koala::Facebook::API.new(session['fb_access_token'])
profile = #graph.get_object("me")
#profile_image = graph.get_picture("me")
friends = #graph.get_connections("me", "friends?fields=id, name, picture.type(large)")
end

Related

Getting list of friends with koala and fb api

Im trying to return the friend list with uid of an authenticated user. however I only get a partial return value, some part of the friends are just left out:
graph = Koala::Facebook::API.new(fb_token)
friends = graph.get_connections("me", "friends")
#some friend action
when I type in the
friends.paging["next"]
in the browser it also returns an empty json array
"data": [ ]
How am I doing it wrong and what is the correct practice?
You need to get the permission 'user_friends' to get friendlist, otherwise you'll receive a empty data:
The field 'friends' is only accessible on the User object after the user grants the 'user_friends' permission.
Try yourself:
https://developers.facebook.com/tools/explorer?method=GET&path=me%2Ffriends&version=v2.5
So, with correct permission you can do:
graph = Koala::Facebook::API.new(fb_token)
result = graph.get_connections('me', 'friends')
to paginate:
next_page = result.next_page
LAST BUT NOT LEAST:
Only friends who installed this app are returned in API v2.0 and
higher. total_count in summary represents the total number of friends,
including those who haven't installed the app.Learn More
You just try this..
This is very simple to you can get all friend count in a single line :)
#graph = Koala::Facebook::API.new(facebook_token)
#friends = #graph.get_connection("me", "friends",api_version:"v2.0").raw_response["summary"]["total_count"]

Retrieve User ID of Facebook App Invitor

In the context of a given Facebook app, suppose User A invited user B to start using it. Once User B accepts to use the app, is there any way to retrieve the ID of User A programmatically (via either PHP/JS SDK) ? This doesn't seem quite documented.
For what it's worth, A/B users are friends, if it's any use.
when user comes following the app request, you can get request id's using
$_GET['request_ids']
then retrieve all the request ids with which you can call graph api to get the corresponding request details like below:
if(isset($_GET['request_ids']))
{
$request_ids = $_GET['request_ids'];
}
$request_ids = explode(",", $request_ids);
foreach($request_ids as $request_id)
{
$request_object = $facebook->api($request_id);
//this $request_object have sender facebook id in the field uid_from
}
If you look here:
http://developers.facebook.com/docs/reference/dialogs/requests/
You can see the object layout. Of note is the data property:
Optional, additional data you may pass for tracking. This will be
stored as part of the request objects created. The maximum length is
255 characters.
In this object you can add your referring UserId and then when the request is claimed, you can then process it on your end.
Hope this helps.

Rails 3.1 - Facebook Page API calls

I have the following working perfectly for calls to Facebook pages with simple URLS like: www.facebook.com/TurbonegroHQ
def get_facebook
#artist = Artist.accessible_by(current_ability).find(params[:id])
if #artist.facebook_url.present?
require 'open-uri'
require 'json'
result = JSON.parse(open("https://graph.facebook.com/"<<#artist.facebook_url).read)
#hometown = result["hometown"]
#band_members = result["band_members"]
#likes = result["likes"]
end
end
However, when a Facebook page URL isn't in this format, but something like https://www.facebook.com/pages/Clutch-The-Bakerton-Group-Weathermaker-Music/16637738637 it fails. Any ideas how I can get around this?
Thanks in advance?
TurbonegroHQ is the username of the page - this is interchangeable with the object ID in the Graph API.
If the page doesn't have a username you need to access it via the ID instead. In that case https://graph.facebook.com/16637738637 is the URL to access
Usually you'll have IDs rather than the URLs as the ID is returned in the API on all the endpoints I can think of

Accessing data using facebook graph API in fb_graph [rails]

I encounter something annoying while playing around with the facebook graph API. I'm using fb_graph which is a ruby wrapper for the facebook sdk. I'm not sure if this issue I encounter is a limitation of the facebook sdk or I should contact the owner of fb_graph.
So, using fb_graph, this is how I would connect to a user:
jane = FbGraph::User.fetch('janeid', :access_token => "janeaccesstoken")
jane.friends will list all of jane's friends. However, if I try to get jane's friends birthday or email, it returns nil. In other words,
jane.friends[0].birthday => nil
But, I can access friends[0] birthday this way:
bill = FbGraph::User.fetch(jane.friends[0].id, :access_token => "janeaccesstoken")
In other words, if I wanted a list of all jane's friends' birthdates, I cannot do this:
#jane.friends.each do |friend|
puts friend.birthday
end
instead, it will have to be something like this
#jane.friends.each do |friend|
friendname = FbGraph::User.fetch(friend.id, :access_token => "janeaccesstoken")
puts friendname.birthday
end
I guess it's not too bad but it does make me wonder why this is the case, because actually only some info is nil. Name, for example, is not.
#jane.friends.each do |friend|
puts friend.name # => works!
end
The answer is here:
https://github.com/nov/fb_graph/issues/237.

I am having problems running Facebook FQL queries that include long user ids

I am having problems running queries with FQL that include a supplied "Large"(beginning with 10000..) User ID
here is an example of one that is not working:
fql?q=SELECT uid, first_name,last_name,pic,pic_square,name
FROM user
WHERE uid=100002445083370
Is there a way to encapsulate the long number so it's passed as a string?
here is another example:
/fql?q=SELECT src_big
FROM photo
WHERE aid IN (SELECT aid
FROM album
WHERE owner=100002445083370 AND type="profile")
ORDER BY created DESC LIMIT 1
Has anyone been able to solve this issue? I am testing the queries in the graph explorer with no luck as well.
I see what the problem is,
The User id I am trying to pass is supposed to be: "100002445083367", but from querying the list of friends and grabbing their User Id, I am getting back "uid":1.0000244508337e+14 which is being shortened to: 100002445083370 (php removing the e+14) throwing off the second query. I need to make sure the id I am grabbing is staying as a string value not a number while I pass it back and forth from PHP and Javascript.
The problem is because of the way PHP handles JSON_DECODE. I had to modify Facebook PHP SDK and add a preg_replace previous to the json_decode. It will make sure json_decode doesn't convert large integers to floats by first converting them to strings.
here is the code:
line 803 from base_facebook.php:
$result = json_decode(preg_replace('/("\w+"):(\d+)/', '\\1:"\\2"', $this->_oauthRequest($this->getUrl($domainKey, $path),$params)), true);
here is more information on the subject:
http://forum.developers.facebook.net/viewtopic.php?id=20846
What do you mean by "not working"?
That query works for me in Graph API explorer but the response is
{
"data": [
]
}
I think that user-id isn't valid; https://www.facebook.com/profile.php?id=100002445083370 gives a "Page not found" error for me.