I have made sure that the user has 'read_insights' permission. I also have 'manage_pages' permission. I receive no errors which is kind of annoying as it woud help.
All I ever get is an empty array. The Graph API works just fine, returning correct data every time.
FQL does not work, and I have never got Insights from it yet. Either they have a bug or I'm doing something wrong.
I have this code to get some Insights:
$('.page-entry>a').live('click', function(e) {
e.preventDefault();
var id = $(this).parent().attr('id');
FB.api(
{
access_token: accessToken,
method: 'fql.query',
query: 'SELECT metric, value FROM insights WHERE object_id = ' + id + ' AND metric="page_impressions" AND end_time=end_time_date("2012-06-01T07:00:00+0000") AND period=period("month")'
},
function(data) {
console.log(data);
}
);
})
The user is logged in, and I can see all of their pages/apps fine. I have the id's and even access_tokens for each page. I tried that for FQL, it didn't work.
Any help appreciated.
Related
I want to check if user is a fan of a specific facebook page, I have tried many times with FQL but the response is always null:
FB.api({
method: 'fql.query',
query: 'SELECT uid FROM page_fan WHERE uid=user_id AND page_id=page_id'
}, function(resp) {
if (resp.length) {
alert('A fan!')
} else {
alert('Not a fan!');
}
}
);
I've tried with user table is running good:
FB.api(
{
method: 'fql.query',
query: 'SELECT name FROM user WHERE uid=me()'
},
function(response) {
alert('Your name is ' + response[0].name);
}
);
I've just checked in https://developers.facebook.com/docs/reference/fql/page_fan/. It seems that facebook does not support FQL, so what can I do ?
I really appreciate your help.
UPDATE
I've just tried Graph API solution but the result is array null:
FB.api('/100005548217775/likes/329849167104448',function(response) {
if( response.data ) {
if( !isEmpty(response.data) )
alert('You are a fan!');
else
alert('Not a fan!');
} else {
alert('ERROR!');
}
});
If you don't have an v2.0 app, you can't use FQL at all, because it's deprecated. Furthermore, you're using the wrong method parameter, which is also deprecated.
The official endpoint for FQL is
/fql?q={url_encoded_query}
where {url_encoded_query} is the url encoded FQL query.
I'd recommend you skip FQL, and go with the standard Graph API funcitonality (which is future proof):
/{user-id}/likes/{page-id}
Note that you'll need the user_likes permission in the access token to be able to get any result.
See
https://developers.facebook.com/docs/graph-api/common-scenarios#pagelikes
I've found my answer:
FB.login(function(response){
if(response.status=='connected'){
FB.api('/me/likes/' + 216524221773305, function(api_response)
{
if(api_response.data.length > 0){
setLocation('<?php echo $this->getConnectUrl() ?>');
}else{
jQuery('#facebook-like-popup').show();
}
});
}
}, {scope: 'user_likes'});
There is one thing that make me confused that is: When I've just liked a fan page then check the result still return Array[]. It seems that Facebook doesn't update data immediately. Not sure what I think is right ?
This will not either work as you need user_likes permissions from facebook, you can try something else, lets say that the page you want to find out if a user liked is
https://www.facebook.com/pg/Poulamecom-159391200925483/likes/?ref=page_internal
1)visit page
2)view source
3)find "likeStatus" inside source, if its true then user liked page, if false then user did not like the page
4)"visit page" could be done automatically by a script and check if likeStatus":true or likeStatus":false
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!
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
I am new in unity and trying to integrate Facebook unity SDK. But i am not able to find current logged-in user name. It will return only userId and accessToken. How to get login name after FB.Login("email")
After you login, you can get the name with:
FB.API("me?fields=name", Facebook.HttpMethod.GET, <your callback here>);
After calling the API
FB.API("me?fields=name", Facebook.HttpMethod.GET, NameCallBack);
get the exact name from the JSON like this:
void NameCallBack(FBResult result)
{
IDictionary dict = Facebook.MiniJSON.Json.Deserialize(result.Text) as IDictionary;
string fbname = dict["name"].ToString();
}
My best guess if I was to look somewhere is to make use of that userId and FQL to query Facebook for the name of the user.
For instance using the Graph API Explorer:
https://developers.facebook.com/tools/explorer
I used the following query:
Select name from user where uid = [userId]
It returned the following JSON string:
{
"data": [
{
"name": "myName"
}
]
}
I think you can use https://developers.facebook.com/docs/php/howto/profilewithfql/ to get an idea of how to query the FB.API to perform a FQL query.
FQL Guide: https://developers.facebook.com/docs/technical-guides/fql/
I have written a code to get all album names to list it. When i write code using javascript
it returned me an empty set.
FB.api('/me/albums', function(response) {
});
I tried Query also . It also returned me an empty set
SELECT aid,owner,name FROM album WHERE owner = me().
I am having album in my account but still it is returning empty set. Please help me in figuring out
include access_token in parameters list
FB.api('/me/albums?access_token=' + accessToken, function(response) { });
Make sure that the user has authorized the app and supply the access token with the query;
FB.api('/me/albums?access_token=ACCESS_TOKEN', function(response)
{
console.log(response);
});