Facebook Graph API + Facebook Pages - facebook

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.

Related

HTTP 400 Error when accessing Friends list in Facebook Graph API

When I access the list of my friends using the URL https://graph.facebook.com/me/friends?access_token=... I can see the list of my friends. I can do this programmatically as well.
Now, when I take the ID of any of my friends and replace it with "me" in the above URL, and paste the URL in the browser, I can see my friend's friends. I am unable to do this programmatically because it is giving me an HTTP 400 ERROR.
Does anyone know why this is possible by pasting the URL and not programmatically?
You can not get the "friends of friends" using the facebook api.
For example, try the simple /me/friends with the Graph API Explorer, it should work fine.
Then, take one of the ids there and try the same with FRIEND_ID/friends and you should get this:
{
"error": {
"message": "(#604) Can't lookup all friends of FRIEN_ID. Can only lookup for the logged in user or the logged in user's friends that are users of your app.",
"type": "OAuthException",
"code": 604
}
}
The error itself is very straight forward and explains exactly what the problem is.
As for why that translates into a 400 error code for you is unclear.
Edit
You can't see the "friends of friends" in the browser as well, the reason that it works for you (probably) is that the user(s) you check the friends for (USER_ID/friends) installed the app that the access token belongs to, from the way you got to that url I assume that the app is the "Test_console".
How to check? Copy the access token from the url (USER_ID/friends?access_token=xxxxx), go to the Facebook Debugger and paste the token in the text field and click "Debug", it will show you info regarding the application.
The user you checked it for probably has that app "installed", if you check it for other users you will get that error when you get to a user who does not have that app "installed".
I hope this clarifies it for you.
I got friends of friends(limited) who are using application. I had same problem. Though it is very late for answering question, it will help somebody. That's why answering this question.
We can get friends of friends those are app users.
$fb_id= user id whose friends of friends required.
Try this fql query.
$query="SELECT uid, name, work_history FROM user WHERE uid IN (SELECT
uid2 FROM friend WHERE uid1 IN (SELECT uid FROM user WHERE uid IN
(SELECT uid2 FROM friend WHERE uid1 = $fb_id ) and is_app_user=1) )";
It needs following requirements:
Your friend needs to be using application.
Permission from application read_stream, publish_stream, publish_checkins.

How to check if someone is liked my fanpage from my website?

I want to check if a user liked my fan-page from my website and if he did i skip the step and if he didnt i want to suggest him to like my fan-page,its all on my website i dont want to put a landing page or something else on my fan-page.all of it is on my website,i don't want to put a landing page or anything else on my fan-page.
Thanks
there are only 2 ways for this:
-) use the edge.create event: https://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/
downside: this can only check right after clicking a like button on your website. so, if the user comes back, you canĀ“t be sure if the like button is still clicked. also, you never get the id of the user, of course
(deprecated)
-) authorization with facebook login > you can check for the user likes > user_likes permission needed. but i doubt that you really want to authorize the user just for this...
the graph api call you could use:
me/likes?target_id=[page-id]
subscription:
permission user_subscriptions needed. graph api call would be the following:
me/subscribedto?user=[user-id]
You need user_likes permission to view like information about the current session user.
refer to: http://developers.facebook.com/docs/reference/login/user-friend-permissions/
then simply use fql to request information.
SELECT page_id FROM page_fan WHERE uid=me() AND page_id=22934684677
If user likes the page graph will return id of page.
Example graph request like for page Facebook.
http://developers.facebook.com/tools/explorer/135669679827333/?fql=SELECT%20page_id%20FROM%20page_fan%20WHERE%20uid%3Dme()%20AND%20page_id%3D20531316728
{
"data": [
{
"page_id": 20531316728
}
]
}
Else array will be empty.
{
"data": [
]
}

In facebook-How can I get if the user is admin of the page using javascript?

I have created application and page in facebook .Then added application into the page and also I have assigned two more user as a admin for that particular page.
I have used the below code for getting admin of that page
FB.api({
method: 'fql.query',
query: 'SELECT page_id, type from page_admin WHERE uid='+id
},
function(response) {
if(response.length){
alert('User is an Admin');
}
else{
alert('User is not an Admin')
}
}
);
This code getting the admin of that page only for current session user(that is me),not an other two person.
but that two person also admin of that page.but this code display "user is not an admin".
I want to display two person also admin of that page.
how to display other two person also admin of that page?
Please give me some suggestion or ideas.
Thanks in advance.
maybe if you allow the manage_pages permission you'll have more luck. Here is a link to the facebook documentation detailing the Page endpoint and what properties you can get from it.
Facebook page documentation - admins.
To get the full list of admins you might have to select all of the pages members and run this test on them :
To check if a specific user is an admin of the Page, issue an HTTP GET
request with the appropriate PAGE_ID, Page access token, and USER_ID
to https://graph.facebook.com/PAGE_ID/admins/USER_ID

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/

How can I count the total friends of a Facebook user by uid?

How can I count the total friends of a Facebook user by uid?
There is a friend_count field against the user table that you can query.
SELECT friend_count FROM user WHERE uid = me()
Just replace me() with the UID of the user that you want to query.
#Scott
This very much works, I didn't expect it to :)
https://graph.facebook.com/fql?q=SELECT friend_count FROM user WHERE uid = 273103345
I tried with a random user id who is not a friend in Graph API. However for few random uids it returned null, I am guessing they were pages.
FQL is now deprecated as of October 2014. Quoting Facebook's FAQ :
As of Tuesday 22nd July 2014, we now return a total_count field within
a summary property in the response to /v2.2/me/friends.
The user/friends endpoint does require the user_friends permission to return an actual list of friends who use your app, however the friend count field will still be returned without this the user_friends permission granted. Sample response:
{
"data": [
],
"summary": {
"total_count": 811
}
}
Full FAQ: https://developers.facebook.com/docs/apps/faq#total_friend_count
COUNT is not supported by FQL:
SELECT uid2
FROM friend
WHERE uid1 = USERID
Since COUNT is not supported you need to get all of the records and then count them in whatever application that is making the SQL call.
If you want to get more information, like names, along with count you can use an inner select to get the friend's extended info:
SELECT name
FROM user
WHERE uid IN (SELECT uid2
FROM friend
WHERE uid1 = USERID)
Here is a related SO question:
FQL result count
References for Friend table:
Query this table to determine whether
two users are linked together as
friends.
Heres my solution... works great!
$myfriends = $facebook->api('/XXXXXXX152466/friends');
$friendcount = COUNT($myfriends['data']) + 1;
Replace XXXXXXX152466 with the facebook profile id#
Using Facebook PHP SDK along with Graph API!
Starting with Facebook API 2.0 you can't get the full list of Facebook friends, only a partial list of friends that also authorized your app.
You can still get the total number of friends though, the Graph API user/friends has a field summary containing the count as total_count
So you can get it using:
FB.api("/me/friends", function (response) {
if (response && !response.error) {
console.log(response.summary.total_count);
}
});
There are lot of way to count the no of friends using Facebook Graph API with Php SDK, out of one i am telling..
$fbuser=$fb->api('/me/friends');
$access_token = $fb->getAccessToken();
$friend_count=0;
foreach($fbuser['data'] as $friends)
{
$friends_count++;
}
For complete tutorial visit this article Getting FB Friends Count