admin管理员组文章数量:1334826
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.
本文标签: javascriptFBapi does not work when called right after FBinitStack Overflow
版权声明:本文标题:javascript - FB.api does not work when called right after FB.init - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742259486a2442244.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论