Facebook get/querying all wall posts (access tokens return completely different values) - facebook

I've been trying to query what people have posted on my wall as a quick metric of closeness. I've tried both FQL and the Graph API to make these queries without a problem...
FQL:
SELECT actor_id FROM stream WHERE source_id = me() AND message != "" LIMIT 50
Graph:
FB.api('/me/feed?fields=from&limit=50', function(feedresponse) { //do stuff });
Testing these in the Graph Explorer when the application is set to "Graph API Explorer" returns the values I want - my own and other's posts on my wall.
However when the application is changed to my own application, "Fingerprint", the values include my likes, my recent friendship notifications, etc... information I don't want returned and that completely pollutes the query.
I've still gotten the correct results by passing the Graph API Access Token like so:
var access_token = "CAACEdEose0cBALLruchzIKJ1ewgvcjpPJmo1amJwbMHGYyuk544om8hDILt0ZBXiUAriiNI7VY2g1OOwlEgV4bVyCWZACOf2djD1rYBTIwxBnmseNHUm6qybvus23F4AShctQl3wu0rpPS5ZBh68ypVoDMgyyXEGnu6uXBbzOJ7Tx43m2xHsseo1oiU7A80oelxphhidgZDZD";
FB.api('/me/feed?fields=from&limit=50', { access_token : access_token}, ...
Though it works - it definitely doesn't seem like the right way. Can anyone explain why the results are different and how I can get my feed without the other bullshit FB sends me when I'm using the User Access Token with my App.

Related

Why does my facebook friendlist request doesn't show as many friends as on my profile?

I'm trying to get my users friends lists by using the facebook graph API doing so:
FB.api('me/friends', function(response) {
console.log(response);
user.friends = response;
});
When I test on my own profile I get 95% of my fb friends (583 friends instead of the 602 I can see on my profile). Thus, I see in the response object there is a "next" url for the pagination function which doesn't return anything when triggered. Why is this happening ? Where does that possibly come from ?
Thanks
If you have a problem with next URL for the pagination, try using the offset and limit parameters in the URI.
For example, instead of making an API call to me/friends, make a call to me/friends?limit=100&offset=0. This will start the list of your friends from an offset of 0 and will display a list of 100 friends on on each page. The next URL will work just fine in this case. You can however increase the limit of the users per page.

How to filter user's news feed to posts/actions from a specific app in Graph API

I have a Facebook app and I want to filter user's news feed to get latest posts/actions that was posted using my app, in Graph API.
I've found this: How to get activity from my app with Facebook Graph API?
This one is okay for getting actions/posts by a specific friend (or the user itself), what I need is almost the same, with the difference that I need this "stream" by all friends of the user that use my app, not a single user.
Then I've found this: Facebook graph api : how to filter app feeds. For this one, the example in the question itself returns empty data for me (with the appropriate access token with read_stream permission). The only answer suggests to use FQL. I've tried SELECT post_id, message, comments, likes FROM stream WHERE app_id = MY_APP_ID and I got this error:
{
"error": {
"message": "Your statement is not indexable. The WHERE clause must contain an
indexable column. Such columns are marked with * in the tables linked from
http://developers.facebook.com/docs/reference/fql ",
"type": "NoIndexFunctionException",
"code": 604
}
}
How do I retrieve the list of all the last (say 100) posts from a specific app by the user and their friends, from the user's scope?
With all FQL queries, your WHERE clause must include at least one indexable field (those are marked with a star next to the field name in the docs) – for performance reasons.
So for the stream table you have a choice between source_id (id of the user whose wall the post is on) or filter_key. You can read the valid filter keys from the stream_filter table – but every user has the filter key nf for what shows up in their news feed. (post_id is also indexable, but of no use for us here.)
So you could try filtering with WHERE filter_key = 'nf' AND app_id = 'YOUR_APP_ID' – that should give you the posts that your active user is able to see in their feed that where made via your app.
You could also try with source_id, filtering that to either being equal to me() (your current user in FQL), or it being in the list of your friends (whose ids you can get from the friend table, again using me(), as a sub-query) - so like this,
… WHERE (source_id = me() OR source_id IN (SELECT uid1 FROM friend WHERE uid2 = me())) AND app_id = 'YOUR_APP_ID'
– but I think that will have less good performance, so I’d try with filter key first and see if that gets you the posts you want.
EDIT: So, as the questioner found out, using /me/home?filter=app_MY_APP_ID works apparently better, and is also easier. Whoever else might need this, they should use this version, since it is more reliable – FQL often gives less results then one would expect.

Facebook graph API 'friends' request now only returning 25 friends per page? What's going on?

My application(game) has been running on Facebook for some time. I start by requesting a friends list via the graph API call: my-uid/friends
asking for user name and profile pic.
Normally I get back a list of all my friends up to a few thousand, until it starts putting friends on to the next page. Which is nice as most of my users get friends in 1 call.
Suddenly however, and with no changes to the app. about 40 minutes ago (18:40 Tuesday (PDT) - 2nd May 2012) I started getting responses with only 25 friends per 'page'!
I can still get my whole friends list using multiple calls, but the game is not currently set up to do that properly. Can anyone tell me why the sudden change? Anyone else seen similar problems and how do I get the list to give me up to 5000 friends per page like it used to.
Reproducible using the Graph API Explorer
I don't know what else to tell you; perhaps the default number returned has changed, but when I try, a call to /me/friends?limit=5000 returns the full list for me (but my friends list is >500 and < 1000 , so maybe it cuts off somewhere along the way)
(Side note: the average number of friends has been found to be ~190 so presumably most users will have less than 500 anyway, and having to page above 500 would be an edge case
In SDK 4.7 you need to pass in a bundle with the number of friends you want to return, I have set it to 5000 as this is the maximum number of friends you can have on Facebook.
You also need to set up your app as a game in the facebook dev console and use invitable friends to get a full friends list
Create your bundle
Bundle bundle = new Bundle();
Add params to your bundle
bundle.putInt("limit", 5000);
Then pass it in to your GraphRequest
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me/invitable_friends",
bundle,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
//Do something with the response
}
}
).executeAsync();
It seems that facebook changed its limit to 25 results in other api calls too (feed, posts, friends, etc), if you request friends without parameters the JSON response shows the following:
"paging": {
"next": "https://graph.facebook.com/USER_ID/friends?format=json&limit=25&offset=25&__after_id=LAST_ID"
}
Anyway you could/should always set limit & offset parameters to prevent this kind of things, limit = 0 will return all your friends list.
https://graph.facebook.com/USER_ID/friends?limit=0
If you are only requesting friends from a normal user the maximum number allowed is 5,000 so the limit should could be either 0 or 5,000 if you are requesting info from a facebook page or other kind of api calls like posts or feed this limit could increase or decrease.
(Update) Facebook fixed this bug so setting limit to 0 returns 0 friends, you should set a positive limit, thanks Dinuz
I think the best thing you can do is to add limit=5000 parameter as Igy says.
However I posted a bug report since this change wasn't noticed or described in the document.
The number of results returned from the /v2.2/me/friends endpoint now defaults to 25.
Friend list now only returns friends who also use your app: The list of friends returned via the /me/friends endpoint is now limited to the list of friends that have authorized your app.
See Facebook changes
Facebook API change log
If you are using GraphRequest() (e.g. in React Native), you can put it directly in the string field, like so :
new GraphRequest(
'/me',
{
accessToken,
parameters: {
fields: {
string: 'id,email,first_name,last_name,friends.limit(5000)'
}
}
...

Facebook Graph API - Finding a users top friends

I am writing a android app that will pull in a list of all the users friends so they can tag them in the photo but displaying a large box of the friends with their photo instead of a list. Because some people have 500+ friends, we all know their are only a handful (maybe 50) that are friends they actively communicate on Facebook by comments or being tagged in photos. I would like to be able to just pull their top xxx friends as it seems Facebook does this same thing on their site, but I just cant find anything in the Graph API to do this task.
Anyone have any pointers?
The other way of doing it is, make a Graph API request for the status messages posted by the user, the friends who have commented or liked his status are the ones with whom he/she interacts the most, doing this is pretty simple, you can use this:
$statuses = $facebook->api('/me/statuses');
foreach($statuses['data'] as $status){
// processing likes array for calculating fanbase.
foreach($status['likes']['data'] as $likesData){
$frid = $likesData['id'];
$frname = $likesData['name'];
$friendArray[$frid] = $frname;
}
foreach($status['comments']['data'] as $comArray){
// processing comments array for calculating fanbase
$frid = $comArray['from']['id'];
$frname = $comArray['from']['name'];
}
}
keep counters as per your choice, and it will be done.
I wanted to do this without requiring additional/extended permissions. I found a fairly decent approximation for my needs was the mutual_friend_count field in the user FQL table.
So I used:
$params = array('method' => 'fql.query',
'query' => 'SELECT uid, pic_square, name
FROM user
WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())
ORDER BY mutual_friend_count DESC
And then just fire that off using the PHP SDK
$friend_array = $facebook->api($params);
I've ran into this same issue in a web app I'm working on, and open-sourced the code I've used, albeit in Ruby:
https://github.com/mikejarema/facebook-friend-rank
This is actually a web service which takes an active access token & user id and (assuming a read_stream permission has been granted) returns a hash of ids to counts which can be used for sorting within your android app.
I suppose since you're running on a smartphone, this let's you offload a series of calls and any call latency to a server somewhere which is running code optimized specifically for the friend ranking task.
In particular, the ranking algorithm looks at a user's 500 most recent interactions (activity feed) and tallies up the frequency of all friends appearing there. The result gives a reasonable ordering of friends, best to worst, and it also works on subsets of friends (eg. sorting mutual friends).
There's lots of room for exploring photo tags, mutual friend counts, and also looking for the type of interactions (eg. a checkin with a friend is probably a better measure of closeness than a like on their status). The project may evolve to encompass some of these considerations.
Here's a sample app using this approach and Friend Rank on the backend, inspect the network calls to see what the API looks like:
http://facebook-friend-rank.herokuapp.com/demo/index.html
If you watch the network traffic from Facebook's iPhone app, you can see they make this FQL call to get the users top 10 friends they communicate most with:
SELECT uid2, communication_rank
FROM friend where uid1 = me()
ORDER BY communication_rank DESC LIMIT 10
Unfortunately this is not available to applications by default. You would need to contact a Facebook engineer to get this field enabled for your application.
I recommend you the following class:
https://github.com/gajus/facebook-friend-rank
It give your friends a score based on user interaction:
'feed_like'
'feed_comment'
'feed_addressed'
'photo_tagged_friend_by_user'
'photo_tagged_user_by_friend'
'photo_like'
'photo_comment'
'friend_mutual'
'inbox_in_conversation'
'inbox_chat'
then it sort the list by score desc.
Hope it helps.
I can think of two direct ways to get a user's "top friends." The first does not require any extended permissions, rather just a valid access token (which as an app owner, you'll have). You make an API call to get the user's feed, which is a connection to the user node.
This call will that user's 25 most recent posts. With any luck, at least some of the items in JSON array returned from this call will be posts from that user's friends. How will know? Well each post comprising the feed, will have an unique id associated with it--whether an Application or FB User is the source of the Post--and each one of those FB ids can be compared against the user's friend list.
The idea of course is that any friends whose posts appear in the last 25 posts of the user's feed, are closer friends than otherwise.
The second technique is to make a call to the Graph API requesting the friendslists connection to the user node. This call requires the *read_friendlists* permission. The name a user gives to a given list is often strongly probative of the importance of the friends comprising that list (e.g., *best_friends*, or whatever); in other words, one or a combination of a couple of those lists will likely give you that user's top friends.

Retrieve Facebook users that like a URL / web page via Open Graph

Is there a way to retrieve the list of Facebook users that have clicked the like button on an external website?
E.g. there is a domain example.com which has been confirmed via Facebook Insights to belong to fbUser1 (using OG meta tags).
Somewhere on example.com there is a page with multiple XFBL like buttons, each one pointing to a further specific URL on example.com, e.g. example.com/xyz, example.com/abc etc.
What I'd like to get is the list of users that liked example.com/xyz and of those who liked example.com/abc.
My intuitive approach would be to look at graph.facebook.com/123456789/likes (where the number is the ID of the domain taken from FB insights), but this always returns an empty dataset:
{
"data": [
]
}
I've also tried getting an OAuth access token from https://graph.facebook.com/oauth/access_token?type=client_cred&client_id=APPID&client_secret=APPSECRET (where APPID and APPSECRET are taken from a FB application that is marked as owning the domain using OG meta tags), but that makes no difference.
I'd also be interested in a non-OpenGraph (i.e. JS SDK, FQL etc.) solution.
EDIT: Using the following code (according to WideBlade's answer below) still gives me an empty list of likes (the second query never returns):
var objectsQuery = "select id from object_url where url in ('http://example.com/xyz', 'http://example.com/abc', 'http://example.com/123')";
var likesQuery = "select object_id from like where object_id = {0}";
FB.Data.query(objectsQuery).wait(function (objectRows) {
console.log(objectRows);
FB.Array.forEach(objectRows, function (objectRow) {
FB.Data.query(likesQuery, objectRow.object_id).wait(function (likeRows) {
console.log(likeRows);
})
});
});
FQL Should do the trick.
$facebook->api_client->fql_query('SELECT user_id FROM like WHERE object_id="OBJECTID"');
Here is the link.
Some general info about FQL.
FQL is initiated using the JavaScript SDK, in this way.
If you can post some sample code-I can try and give some more specific help.
A note should be made-once you've got the user ID's, you can just query the names and get them in a list.
EDIT: To get the URL of an object you should query this table using fql.
This cannot be done. Facebook does not allow you to query for specific user ID's of people who have liked a certain page. This is because of privacy concerns.
See the disclaimer on this page https://developers.facebook.com/docs/reference/fql/like/