How to search for friends around me using Facebook API? - facebook

Using GraphAPI, I see you can request location of a specific friend, or search for friends by criteria.
However, I couldn't find a way to search for friends in my area. I want to perform a search and get a results of friends who are in a radius of X meters from me.
I know I can fetch each friend manually and check its location, but it seems a bit inefficient so I'm looking for a batch search,
EDIT: I've found a similar question (and answer) using FQL: facebook fql query similar to friends checkins

Well, you partly answered yourself there...
When you query about a user friend, you get his location as well since it's part of the User object (assuming you have the right permissions of course): http://developers.facebook.com/docs/reference/api/user/
The problem, as you wrote, is that you'll need to send a request to facebook per friend, which is not the right way to go about this.
Thankfully, facebook already thought of that and they came up with the "Batch Requests": http://developers.facebook.com/docs/reference/api/batch/ which let you do exactly that.
EDIT
Now that I think of it, it's simpler than I wrote before, you don't even need the batch requests since you can get all of the user friends in one request to: me/friends.
Assuming you have the user granted you the needed permissions each user object in the friends list will have the location object as well.
For each friend calculate if his location is in the desired radius.

Also, you can retrieve friends location and hometown using this Graph API endpoint: /me/friends?fields=location,hometown,name
But you will need the friends_location and friends_hometown permissions.
Read more: https://developers.facebook.com/docs/reference/login/extended-profile-properties/

Related

Search friends within Facebook friends list using Graph API

How do we search for a user by name within user's friends list. I"m able to fetch whole friends list using this api...
"https://graph.facebook.com/v2.9/#{facebook_id}/friends?access_token=#{access_token}"
But I'm not sure how do we query within friends list? Does Facebook support that?
Some SO answers says to fetch the whole friends list and search within the list ourself but I guess this may not be the right solution as users friends might grow we can't cache our result anywhere and fetching whole list every time also would not be a efficient solution. Does anyone knows any other work around?
Please guide me in right direction.
Thanks.

How to retrieve the IDs of Facebook pages (places) located in a specific location and liked by a user's friends?

I know one solution would be to retrieve the IDs of places located in a specific location using the Facebook Graph API, to retrieve the IDs of all the Facebook pages liked by my friends and then to pick only the IDs of pages in the location that my friends liked (using the "social context" that enables me to know how many friends liked a page). But it doesn't sound like the best solution to me (it represents LOTS of data).
Another solution could be to imply dependencies between operations in the request using the JSONPath expression format (https://developers.facebook.com/docs/graph-api/making-multiple-requests/) but as said in the documentation "for security reasons filter and script JSONPath constructs are not allowed in the JSONPath expression", which limits significantly the usefulness of this strategy.
So I tried to use Batch Requests:
curl \
-F 'access_token=...' \
-F 'batch=[{"method":"GET", "name":"likes-ids", "relative_url":"me/friends?fields=likes{id}"}, {"method":"GET", "relative_url":"search?type=place&center=48.85,2.35&distance=1000&ids={result=likes-ids:$.data.*.likes.data.*.id}"}]'
There are 2 issues in each batch request:
Weirdly enough, "me/friends?fields=likes{id}" gets me some likes of
some of my friends (I think it's possible to get all the likes of
all my friends as friends_likes has been deprecated, correct me if
I'm wrong)
Even for the user's friends' likes I manage to
retrieve, I don't get anything from the second batch request.
NB: The user's friends shouldn't need to login on my app (meaning I don't have the user_likes permission for them).
I'd be grateful if anyone could help with some remarks or ideas.
You need the friend to grant the user_likes. You are probably using the Graph API Explorer so your friends might have been testing and granted the user_likes permission (thus why you see some and not all)
A user access token with user_likes permission is required to see all pages liked by that person.
https://developers.facebook.com/docs/graph-api/reference/v2.2/user/likes
Notice it doesn't say friends.
For the second part, are you sure the ids returned from the first part of the batch have ids for places? For each friend object, only 25 likes are returned, you aren't guaranteed that the likes you get are pages of type place.
Also search?type=place&center=48.85,2.35&distance=1000&ids=, I don't this does what you think it's supposed to do. ids seems to override the type,center and distance.
e.g. search?type=place&center=48.85,2.35&distance=1000&ids=375093055847509
is the same as
search?ids=375093055847509
Seems like a bug here or an intended behaviour that as along as ids is returned override every other parameter.
So overall no, this batch call will work, the results you are returning are incorrect for your desired outcome.
So the only way is indeed the initial plan you had but in any event the query will only work for friends who are using your app.
https://developers.facebook.com/docs/graph-api/reference/v2.2/page.context/friends_who_like
Only friends who use the requesting app will be returned.

How to get a list of people who have checked into a place in Facebook

I'm trying to use Facebook API to get a list of people who checked into a place.
When typing in the search box in Facebook UI People who have been to THE_PLACE I get a long list containing many people, also people who are not my friends or friends of friends.
However, when I try to get the same information from the Facebook API, I get a list that only contains my friends who checked in. The FQL I used is the following (and also some other variants that didn't do better): SELECT author_uid FROM checkin WHERE target_id = THE_UID_OF_THE_PLACE
Am I doing something wrong or is this simply impossible to get via API the same information I can access through the UI?
For privacy reasons, this is not possible at all. Also, FQL is deprecated. I have answered a similar question here: facebook graph API - get last checked users to place/page

Get all posts on a friend's wall

I'm trying to find an API call (or set of calls) that will allow an app to get the posts that one would see if viewing a friend's wall. Either a REST call or a FQL call would do.
I tried /feed and /posts, compare the results with what I see on my friend's wall, and the results I get from it are incomplete.
I know this is possible because apps like Friendly are able to do it.
Any hints?
Well, there's two different API endpoints for querying the posts of a given user; home and feed. Home includes posts from other people and pages (basically what you see when you log in and go to your home page) and feed is the stuff the user is sharing. Assuming your application has authenticated the user and they've allowed the read_stream permission, you can then make queries to the Graph API using their access token:
https://graph.facebook.com/{SOME_USER_NAME}/home?access_token={SOME_ACCESS_TOKEN}
and
https://graph.facebook.com/{SOME_USER_NAME}/feed?access_token={SOME_ACCESS_TOKEN}
The only hint I could give you is that "incomplete" is pretty standard with the Facebook API. You can only do the best you can with what they give you. Counts will be wrong. Data will be wrong. It's a fluctuating, moving target, so code to that fact.
You can interact with posts by retrieving the read_stream and publish_stream for a given User's ID.
Once you have the ID of the User that you are interested in you can use FQL to retrieve posts from streams that you have the permissions for.
This stackoverflow answer goes into more detail and contains further links to the documentation on the Facebook Developers site.
We do it like this:
1) we use Facebook Platform PHP5 client
2) then what you do is:
$this->facebook = new facebook($key, $secret);
$out = $this->facebook->api_client->call_method("facebook.stream.get", array('viewer_id'=>0, 'source_ids'=>$uid, 'limit'=>$limit));
then just operate with out, which contains all posts on asked page.
but, afaik, it will work only with pages, not with normal users. but dig that way to get answer.
You should make a graph call to https://graph.facebook.com/< FBID >/statuses?access_token=xxxxx
Here access_token shall have read_stream permission and offline_access permission ..

Facebook Graph API: user gender

The old Facebook API provided the user sex/gender as part of the default user data. Apparently the new Graph API does not provide that information, even though the documentation says that it does.
I've heard people say that you need to request special permissions to get it and other pieces of data, but I have not been successful in getting it to work.
Does anyone have an example, using the Facebook Graph API, of how to get the user's gender and/or location (city/state/country/whatever)?
You need user_location permission to get the location according to the API reference.
GET https://graph.facebook.com/USER_ID?fields=gender,location
The Graph API tool explorer is a really handy tool for checking the output.
Just FYI, location (City and state only, country is still missing) and gender have been added to the Graph API sometimes today.
I was testing an app tonight and noticed there was a couple more fields :)
I see this has an open bounty even though the question is really really old.
You can get the gender of any user using Open Graph even without any access_token.
GET https://graph.facebook.com/userid
json_decode the data
gender will be in the `gender` element of the returned array.
EDIT : user location, hometown, country etc are no more there in the /me. seems like Fb removed those, even if they are public.
Hope this helps
As far as I can tell some permissions like user_location simply aren't working at the moment. I can get email permissions and some others but for most of the user_... ones, nope. Yes they are wrong about gender as part of default data and there appears to be no permission for it either. So use the old api until the kinks in the new one get worked out.