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

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/

Related

How to get "news main stream" from the main facebook site?

How to get the general information listed on the home page?
Facebook makes a request to
https://www.facebook.com/ajax/pagelet/generic.php/LitestandMoreStoriesPagelet?
with params:
ajaxpipe
ajaxpipe_token
no_script_path
data
__user
__a 1
__dyn
__req
__rev
__adt
And although it returns a json (inside a script with conditions...) is not a clean answer.
Is there another way to get that data? I do not want to use the sdk.
If you want the 'News feed', you'll want to use the /user/home/ API endpoint:
https://developers.facebook.com/docs/graph-api/reference/v2.1/user/home
e.g. https://graph.facebook.com/me/home?access_token=VALID_ACCESS_TOKEN will just grab your own News Feed.
You can also filter by any of the 'filter_key' values returned from the FQL query (use the Graph API Explorer and API version 2.0):
SELECT filter_key, name, type, value FROM stream_filter WHERE uid = me()
e.g. adding the query string filter=app_2305272732 will return a feed of things from the 'Photos' app (i.e. photos added to Facebook by your friends)

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

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.

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: ID of user profile picture

Is there a way to get the ID of the picture that is the current profile picture for a user?
Edit: OK, seems there is no trivial way to get the ID!
However, it appears from random testing that every user has a single album with attribute type="profile", and the first image in this album is always the profile picture, so it is possible to get the ID of the profile picture that way. Can some more experienced with Facebook confirm that this is indeed always the case?
You can get the users public profile photo from the following url:
https://graph.facebook.com/[id]/picture
Reference:
http://developers.facebook.com/docs/reference/api/
The answer seems to be yes
e.g., the photos in an album have an ID (the profile photo is a different object though, which a different FB ID). An ID for every object is a core concept of FB's new graph API: Every object in the social graph has a unique ID.
Indeed, data request i have ever made through the FB Graph API returns (when successful) a response in the form of a JSON array, comprised of nested ID-Value pairs, e.g. the education field from a User object:
"education": [
{
"school": {
"id": "126533127390327",
"name": "Massachusetts Institute of Technology"
},
"year": {
"id": "140829239279495",
"name": "1992"
},
"concentration": [
{
"id": "192578844099494",
"name": "Computer Science"
}
],
"type": "College"
}
The profile photo is a connection of the User object (i.e., every FB object has Fields and Connections).
According to the table in the relevant FB Developer Page, a call to the Graph API requesting picture (user's profile photo(s)) returns a string which is the URL for the user's profile picture.
But why doesn't this same call return the user's profile photo ID?
The reason i suppose is that the URL returns:
Graph API : User Properties
The user's profile photo is not there, but in an adjacent node:
Graph API : User : Connections : picture
(See FB Documentation for Graph API structure here).
When you make that API call, examine the Request Headers, and in particular, the Request URL; when you do, you'll see something like this:
http://profile.ak.fbcdn.net/hprofile-ak-snc4/32093_100005743929541_5467982_q.jpg
The string between the underscores (100005743929541) is the user ID. this is easy to verify by making another call to the Graph API. From your browser location bar (assuming you are logged into Facebook) just enter:
https://graph.facebook.com/me
So again, the first item in that JSON string is the user's FB ID. Given that, it seems to me that if user profile ID does indeed have its own ID, then that string (user's FB ID) plus the two smaller adjacent strings of integers on either end of the ID, should be it--in other words, 32093_100005743929541_5467982 in the Request URL above.
Finally, perhaps the best way to answer this is by using the new Graph API Explorer. (I just tried to verify this, but my requests are hanging.)
There is one more simple way to accomplish this task.
Fetch all facebook albums by https://graph.facebook.com/me/albums?access_token=.
Loop into the albums and check if object type == 'profile'.
Get the value of object cover_photo where type == 'profile'.
This value of cover_photo is the id of profile pic of the user.
You can check it by https://graph.facebook.com/<cover_photo value>?access_tokne=
I don't know if you still need this but this is the way I did it in PHP:
$albumsfb = $facebook->api('/me/albums/?fields=type');
$albumsfb = $albumsfb['data'];
foreach ($albumsfb as $albumfb) {
if ($albumfb['type']=='profile'){
$albumprofile = $albumfb['id'];
break;
};
}
$photoprofilefb = $facebook->api('/'.$albumprofile.'/?fields=cover_photo');
$photoprofilefb = $photoprofilefb['cover_photo'];
$photoprofilefb = $facebook->api('/'.$photoprofilefb);
$fotoperfilfburl = $photoprofilefb['source'];
Instead of "me", put the user id that you want?
http://graph.facebook.com/<SOCIAL-ID>/picture?type=<small|normal|album|large|square>
SOCIAL-ID = Facebook returned social id.
small|normal|album|large|square = size of image.
I recently tried to like one of my friend's profile pictures from the Graph API explorer
Here's how I got the id of his current profile pic:
fields=friends..fields(albums.fields(photos.fields(id,name,link),name))
If you have used the Graph API explorer, you might be aware of the above line of code that's generated in the textEntry when you tick the checkboxes. The limit size is up to you, I just kept it 1 to receive the JSON object quickly (without the limit, it took me 5 minutes to get the JSON object containing a lot of information).
So here are the things you need to check in the Graph API (maintain the order):
Friends
Within Friends Albums
Within Albums Name and Photos (Within Photos the id and name of the photo)
I made a PHP script to like all the profile pictures of my friends :)

Facebook Graph API + Facebook Pages

Using Facebook's Graph API, given a username xyz (assuming they've authenticated my site), how do I get a list of all of the facebook pages that the user administers?
The accounts property on the user object says:
The Facebook pages owned by the current user. If the manage_pages permission has been granted, this connection also yields access_tokens that can be used to query the Graph API on behalf of the page.
http://developers.facebook.com/docs/reference/api/user
After getting access token you can get all details of list of all of the facebook pages that the user administers.
https://graph.facebook.com/[FACEBOOKUSERID]?metadata=1&access_token=
The output will look like
{
"name": "Facebook Developer Garage Austin - SXSW Edition",
"metadata": {
"connections": {
"feed": "http://graph.facebook.com/331218348435/feed",
"picture": "https://graph.facebook.com/331218348435/picture",
"invited": "https://graph.facebook.com/331218348435/invited",
"attending": "https://graph.facebook.com/331218348435/attending",
"maybe": "https://graph.facebook.com/331218348435/maybe",
"noreply": "https://graph.facebook.com/331218348435/noreply",
"declined": "https://graph.facebook.com/331218348435/declined"
}
}
}
Use an fql query! That is the best way to get the pages for which the user is admin. You will also be able to restrict the names also. i.e pages with empty names A sample fql query which gives you the page details as well.
SELECT page_id,page_url,name,pic_square FROM page WHERE page_id IN (SELECT page_id FROM page_admin WHERE uid = " + **UserId** + ") and name!=''
UserId -- Id of the admin
NOTE
This method will not work from version 2.1 of graph api as fql was deprecated from that version onwards
I found the answer, you need to use FQL, passing in the appropriate access_token:
https://api.facebook.com/method/fql.query?query=SELECT%20page_id%20FROM%20page_admin%20WHERE%20uid=XXXX&access_token=YYYY
Here's what I use. It works perfect
$pages = $facebook->api(array('method' => 'fql.query','query' => 'SELECT page_id FROM page_admin WHERE uid = '.$uid.''));
foreach($pages as $k=>$v) {
echo 'page id#:'.$v['page_id'].'<br/>';
}
This is of course after you have the session created for the fb user! The $uid would be the profile id# of the specific facebook user your returning of a list of managed pages for.
#rmorrison - This is not graph API though. With the new "like" addition, you can use this URL: h ttps://graph.facebook.com/me/likes?access_token=blah! or h ttps://graph.facebook.com/USER_ID/likes?access_token=blah!
Fql is the best way to get the pages of the user for which he is the admin. As the others like likes of user will give all the pages that the user like.
You can get list of pages from a simple Facebook Graph API Hit.
https://graph.facebook.com/{user-id}/accounts?access_token={access-token}
It will give a list of pages for user which he/she has created.
access_token is valid for an hour or less. What should someone do in order to get the details even after. I was trying to result get the result from
https://graph.facebook.com/v2.3/search?q=coffee&type=place&center=37.76,-122.427&distance=1000&access_token=xyz
This worked for an hour or so but how do i get the result after an hour...without going for a login.