FaceBook API for friends count - facebook

There is the question about fetching count of user friends via FaceBook API.
I want to get the number of friends only have USER_ID of the any facebook user. As I know I can get it using FQL like:
SELECT friend_count FROM user WHERE uid = X
But for some users this value is null or there is no value. Please advice is there another way to get this value?

That user might be keeping that information private. In that case, you cannot fetch that information.

You just check the following link. You need to generate token (by clicking on Get Token you will get it) with data you want from facebook API, need to select friends option to get the friend count.
https://developers.facebook.com/tools/explorer/?method=GET&path=me%3Ffields%3Did%2Cname%2Cfriends&version=v2.8
FB.api(
'/me',
'GET',
{"fields":"id,name,friends,link,email,gender,picture,first_name,last_name,cover"},
function(response) {
// Insert your code here
}
);
after executing this you will get this in response as
{
"id": "XXXXXXXXXX",
"name": "Test test",
"friends": {
"data": [
],
"summary": {
"total_count": 14
}
}
}
total_count will be your friend count

Related

How to get instagram username from new facebook graph API

Looking at the new Instagram Graph API - there is no direct way to retrieve username once a user is logged in. Although it says in the official document that a call like
"https://graph.facebook.com/v3.2/17841405822304914?fields=biography%2Cid%2Cusername%2Cwebsite&access_token=EAACwX..."
should return the following:
{
"biography": "Dino data crunching app",
"id": "17841405822304914",
"username": "metricsaurus",
"website": "http://www.metricsaurus.com/"
}
This currently returns an error, and looks like there is no such option to get only username of an instagram business user.
If you are talking about fetching username of an instagram business account which is linked to your facebook page, you can use the below curl
curl -i -X GET "https://graph.facebook.com/v3.2/me/accounts?fields=instagram_business_account%7Busername%2Cname%2Cig_id%7D&access_token=<access_token>"
Please replace access_token with your user access token in the above curl.
Sample reponse which you will get for the above curl is as below :
{
"data": [
{
"instagram_business_account": {
"username": "<instagram user name>",
"name": "<instagram display name>",
"id": "<corresponding facebook graph id for instagram profile>",
"ig_id": <instagram profile id>
},
"id": "<facebook page id>"
}
],
"paging": {
"cursors": {
"before": "MTc2Njg1MTU2MTI1ODA1",
"after": "OTg3MzU1NzExNDUwMDg3"
}
}
}
If it helps to someone.. Now its better to do it like this GrapAPI v10.0
FB.api('/' + pageID + '?fields=instagram_business_account{id,name,username}',
function (response) {
// TODO: whatever you want
},
{ access_token: accessToken }
);
EDIT: I wouldn't do it this way anymore. Its very old code, I would wrap it somehow and I am not sure this still works..
I was able to solve this only by using the old api.
According to their documentation, although the old API is about to become deprecated, as of now, it is meant for small businesses or personal apps.
The new Instagram Graph API is "An API for accessing data in large- and medium-sized Instagram Business Accounts"
Looks like FB doensn't know it's way around.

Get user id and name from comment on Fan Page Wall

Since Facebook API update to v2.11, I cannot get User Id and Name from comments on FanPage Wall Post. Anyone can help/explain?
Usually I used this :
https://graph.facebook.com/[POST_ID_PAGE]/comments?order=reverse_chronological&access_token=[YOUR_ACCESS_TOKEN]
And the result just like :
{
"data": [
{
"created_time": values_date_TMZ,
"from": {
"name": USER_NAME,
"id": USER_ID
},
"message": THE_COMMENT,
"id": THE_COMMENT_ID
}
}
You need a Page Token now, to get user names - check out the changelog:
https://developers.facebook.com/docs/graph-api/changelog/version2.11#gapi
I assume you are using an App Token at the moment.
Edit: Of course it has to be a Page Token of the Page with the comment. And of course you need to manage the Page - else you would not even get a Page Token.

FACEBOOK API: Get likes of particular post on page

I want to get likes on post on the page: "The Jungle Book"
I am able to get the likes of the page using url:
https://graph.facebook.com/" + pageId + "?fields=likes&access_token=" + ACCESS_TOKEN
I have read the documentation
and find that i will get the likes(total count) of the post only returned when the flag summary=true is set
The response should look like this
{
"likes": <somecount>,
"id": "151505938209071_1507814655911519"
}
I am able to get the likes on page but not able to get total number of likes on the particular post on that page.
Can anyone help me regarding how I should set the URL or is there any other way to do it.
From what you described it sounds like it should be like this:
https://graph.facebook.com/" + pageId + "?fields=likes&summary=true&access_token=" + ACCESS_TOKEN
The above is your existing URL with summary=true in between the fields=likes and access_token parameters
However you may be interested in this answer
Quote:
Couldn't find this in the documentation but multiple calls to the API are not necessary. You can use summary when querying a feed or multiple posts. Specify this in the fields parameter.
https://graph.facebook.com/PAGE_ID/feed?fields=comments.limit(1).summary(true),likes.limit(1).summary(true)
This will return a response like this.
{
"data": [
{
....
"summary": {
"total_count": 56
}
...
},
{
....
"summary": {
"total_count": 88
}
...
}
]
}
This will be much faster than making individual requests for each object just to get the number of comments or likes.

Missing Photos from Timeline Photos album

I'm using Facebook Graph API to get all the photos of a user graphPath: me/albums and find something weird with Timeline Photos album.
Photos exist in Timeline album on Facebook, but not returned from API call graphPath/{timeline_photos_album_id}/photos, or they are returned with lower number of photos.
What is the problem?
Logs:
Calling for albums graphPath: me/albums:
.....
{
"can_upload" = 0;
count = 3; !!!!!!!!!!!
"created_time" = "2013-06-18T10:43:27+0000";
description = cool;
from = {
id = 100006100533891;
name = "Mike Mike";
};
id = 1387760311437307;
link = "https://www.facebook.com/album.php?
fbid=1387760311437307&id=100006100533891&aid=1073741829";
name = "Timeline Photos";
privacy = everyone;
type = wall;
"updated_time" = "2013-06-18T10:47:53+0000”;
},
.....
Calling for album photos graphPath/{timeline_photos_album_id}/photos:
{
data = (
);
}
Unfortunately "it is not a bug, it is by design".
I tried the following (with "user_photos" permission) and it works:
Fetch the album photo count for my user:
https://graph.facebook.com/me/albums?fields=id,count&access_token={access_token}
The result is
{
"data": [
{
"id": "{album_id}",
"count": 161,
"created_time": "2010-11-05T15:41:42+0000"
},
....
}
Then query for the photos of this album:
https://graph.facebook.com/{album_id}/photos?fields=id&limit=500&access_token={access_token}
The result is
{
"data": [
{
"id": "{photo_id1}",
"created_time": "2013-12-29T09:59:52+0000"
},
{
"id": "{photo_id2}",
"created_time": "2013-12-17T10:40:26+0000"
},
...
}
I count the correct number of 161 ids in the result. So everything seems to work fine.
You can also test this via FQL:
https://graph.facebook.com/fql?q=select+pid+from+photo+where+album_object_id+=+{album_id}+limit+1000&access_token={access_token}
there have been changes in the FB API recently, did you try to add the fields you want to get back in the result from the API? In the past, API always returned "standard fields", newest you always have to provide a list of fields of what you want to have back in the result such as ....fields=name,id...
I think you are missing the user_photos permission in the Access Token. The end point:
/{timeline_photos_album_id}/photos is working perfectly fine for me when I tried it using the Graph API Explorer to retrieve photos from my Timeline photos album. And, I don't think that it is necessary to provide the fields.
I'll suggest you should try the same using the Graph API Explorer. Just press the Get Access Token button on the top right and check the user_photos permission to test your request.
In case you are getting lesser results, you can try your query using pagination options limit and offset. This sometimes returns more results as compare to normal(nothing about this on docs, just my personal experience). Something like:
{timeline_photo_album_id}/photos?limit=500&offset=0
But again, as you pointed out, there are strange things going on with the API. Take a look at this article. This article refers to it as an expected behavior of the API!

Facebook Graph API. How to get likes for comment?

I need to get ids of people who liked some comment.
For example, I have the post with id 246595548763188_464132103676197 , and this post has comment with id 464146663674741.
Then, I use Graph Api Explorer (https://developers.facebook.com/tools/explorer/)
Permissions: user_groups
Graph API URL: https://graph.facebook.com/464146663674741
Result:
{
"id": "464146663674741",
"from": {
"name": "Rostyslav Paranko",
"id": "100001288191932"
},
"message": "Українську мову доконають пуристи.",
"can_remove": false,
"created_time": "2013-06-03T19:10:57+0000",
"like_count": 8,
"user_likes": false
}
As you can see, like_count has value 8, but I can't see people who liked this comment (with ids and names). https://graph.facebook.com/464146663674741/likes returns empty array.
So, is it possible to get comment's likes using Graph API (or, maybe, FQL Query)?
Get the comment ID and make this Graph API request:
https://graph.facebook.com/COMMENT_ID?fields=likes
The comment ID is in fact the concatenation of:
the POST_ID (246595548763188_464132103676197),
the COMMENT_ID (464146663674741),
which would give in your case: 246595548763188_464132103676197_464146663674741.
So, this should work:
graph.facebook.com/246595548763188_464132103676197_464146663674741?fields=likes