Issues FB.api('/me/likes/ - facebook

I have problem with fb api.
FB.api('/me/likes/pageid',function(response) {
console.log(response.data);
});
i have
Object
category: "Entertainment"
created_time: "2012-09-17T16:56:56+0000"
id: "pageid"
name: "site.com"
but on one computer i have undefined Object { data=[0]}
I logged in on both in facebook. any ideas?
in FF i have
An active access token must be used to query information about the current user.

You have to specify a valid access_token
This is the right way to do that
FB.api('/me/likes/pageid', {access_token: "YOUR_ACCESS_TOKEN"}, function(response) {
console.log(response.data);
});
You have to add "user_likes" to the scope of your app to retrieve the result

Related

Facebook api graph all photos giving empty array

I'm trying to get all photos from my facebook pefil but all i got is a empty array data or only the perfil photo, i think that with could be my passport facebook error but with the facebook api im geting empty array too,
i try this,
https://graph.facebook.com/USERID/photos?access_token=USERTOKEN
i dont know what i'm doing wrong, i tried with photo type uploaded and doesn't work too, i just wanna all perfil photos.
reponse:
{
"data": []
}
If you are still receiving an empty array after obtaining the user_photos permission, try adding type=uploaded (options are uploaded or tagged), e.g.: /me/photos?fields=images&type=uploaded&access_token=...) or in the JS SDK:
FB.api("/me/photos", { fields: "images", type: "uploaded" }, (resp) => { console.log(resp) });
(Note you can always pass "me" instead of a user ID to get info for the current user)

Should I use information stored in a cookie for indexing purposes?

I am creating my first major app, and I thought of a way to optimize query performance. I am not sure though whether I should go through with it.
Here is a description of my approach.
Every time a user account is created, that user is assigned a random number between 1 and 10, which is stored in their user document. The number is called a number-Id. Here is how the user schema will look like:
let User = new Schema({
/// I left out all the other fields for clairty sake
numberId: {
type: Number,
default: Math.floor(Math.random() * 11),
index: true
}
}
Every time a user creates a blogpost and post, their number-Id is referenced inside the document of that blogpost and post. This is to make querying much faster by indexing the users number-id. Here is how the document of a blogpost would look like in MongoD:
{
"title": "my Blog Post",
"_id": "ObjectId("594824b2828d7b15ecd7b6a5")",
/// Here is the numberId of the user who posted the blogpost, it is added
/// to the document of the blogpost when it is created.
"postersNumberId": 2
/// the Id of the user who posted the blogpost
"postersId": "59481f901f0c7d249cf6b050"
}
Let's say I want to get all the blogposts made by a specific user. I can optimize my query much faster by using the number-Id of the user in question as an index, given that their number-Id is referenced in all the blogposts and comment posts they make.
BlogPost.find({postersId: user_id, postersNumberId: user.numberId});
It seems like this approach warrants that I store the users number-id in req.user in order for it to be readily available whenever I need it to optimize queries. So that means I would have to store the users data in a cookie via passport:
passport.serializeUser(function(user, done){
done(null, user._id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function (err, user){
if (err || !user) return done(err, null);
done(null, user);
});
});
Given this approach, I could now use all the information stored in the cookie, particularly the numberId, to optimize queries that retrieve the comments and blogposts a user makes:
BlogPost.find({postersId: req.user_id, postersNumberId: req.user.numberId});
However, I am using json-web-tokens to authenticate the user rather than cookies. So I will have to use a cookie to store the number-Id for indexing purposes in addition to using JWT for authentication. I've heard, however, that having cookies is bad for scalability, so I am worried that storing the users number-Id in req.user will eventually impact performance.
Should I continue with this approach, or no? What are the performance implications?
In addition to authentication JWT has a payload, which can be used to store additional information within the generated token itself:
var jwt = require('jsonwebtoken');
var token = jwt.sign({
data: {
numberId: 7
}
}, 'jwtSecret', {
expiresIn: '1h'
});
For retrieval:
jwt.verify(token, 'jwtSecret', function(err, decoded) {
if (err) {
console.log(err)
} else {
console.log(decoded);
//{ data: { numberId: 7 }, iat: 1498350787, exp: 1498354387 }
}
});

How to check if user is a fan of facebook page by using FQL?

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

Facebook album Query and graph api returns empty set

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);
});

Facebook FQL API returning empty array

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.