FB.api does not work when called right after FB.init. Here is the code snippet I use:
window.fbAsyncInit = function() {
FB.init({
appId : window.APP_ID,
status : true,
cookie : true,
oauth : true,
channelUrl : window.MASTER_URL + "channel",
frictionlessRequests : true
});
window.COMPANY.init();
};
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
And here is my COMPANY.init() and COMPANY.fetchAllFriends():
friends: new Array(),
init : function() {
// TODO
COMPANY.fetchAllFriends();
FB.Canvas.setSize($(document).height());
FB.Canvas.setAutoGrow();
},
fetchAllFriends : function() {
FB.api('/me/friends', function(response) {
if (!response || response.error) {
return;
}
COMPANY.friends = new Array();
$.each(response.data, function(index, value) {
COMPANY.friends.push(value.id);
});
});
},
where at fetchAllFriends response contains error which says "An active access token must be used to query information about the current user.". I made sure (via the browsers debugger) that FB.init is called before that call of FB.api.
Any help will be appreciated!
FB.api does not work when called right after FB.init. Here is the code snippet I use:
window.fbAsyncInit = function() {
FB.init({
appId : window.APP_ID,
status : true,
cookie : true,
oauth : true,
channelUrl : window.MASTER_URL + "channel",
frictionlessRequests : true
});
window.COMPANY.init();
};
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
And here is my COMPANY.init() and COMPANY.fetchAllFriends():
friends: new Array(),
init : function() {
// TODO
COMPANY.fetchAllFriends();
FB.Canvas.setSize($(document).height());
FB.Canvas.setAutoGrow();
},
fetchAllFriends : function() {
FB.api('/me/friends', function(response) {
if (!response || response.error) {
return;
}
COMPANY.friends = new Array();
$.each(response.data, function(index, value) {
COMPANY.friends.push(value.id);
});
});
},
where at fetchAllFriends response contains error which says "An active access token must be used to query information about the current user.". I made sure (via the browsers debugger) that FB.init is called before that call of FB.api.
Any help will be appreciated!
Share Improve this question edited Dec 13, 2011 at 18:32 Matt Ball 360k102 gold badges653 silver badges720 bronze badges asked Dec 13, 2011 at 18:30 Martin AsenovMartin Asenov 1,2982 gold badges21 silver badges38 bronze badges2 Answers
Reset to default 6Ok, I fixed my problem. I changed:
window.fbAsyncInit = function() {
FB.init({
appId : window.APP_ID,
status : true,
cookie : true,
oauth : true,
channelUrl : window.MASTER_URL + "channel",
frictionlessRequests : true
});
window.COMPANY.init();
};
to:
window.fbAsyncInit = function() {
FB.init({
appId : window.APP_ID,
status : true,
cookie : true,
oauth : true,
channelUrl : window.MASTER_URL + "channel",
frictionlessRequests : true
});
FB.Event.subscribe('auth.login', window.COMPANY.init);
};
in case anyone else want to know. What I found is that the FB JS SDK has not formed the user session by the time when FB.init returns, so what you need to do is to subscribe to the 'auth.login' event with your init function. More info here
You don't appear to be checking the status of the user. All you are doing is initializing Facebook, you don't even know if the user is logged into Facebook or has a Facebook account. You need to call FB.getLoginStatus and check if they are "connected". If they aren't connected, there is no possible way to get an access token. If they are connected, then you get an access token.