通过 Javascript SDK api 调用进行授权
我正在使用 javascript sdk,我不清楚何时可以进行 api 调用。 FB.api 在 FB.getLoginStatus 函数中工作得很好,但下面的 FB.api 调用本身会打印名称“未定义”。显然我错过了一些基本的东西。我应该以某种方式使用访问令牌吗?
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
FB.api('/me', function(response) {
// SUCCESS
alert('Your name is ' + response.name);
});
}
});
FB.api('/me', function(response) {
//FAILURE
alert('Your name is ' + response.name);
});
I'm using the javascript sdk and I'm unclear about when I can make api calls. FB.api works just fine within the FB.getLoginStatus function, but the FB.api call on its own below prints the name 'undefined'. Clearly I'm missing something fundamental. Should I be using the access token in some way?
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
FB.api('/me', function(response) {
// SUCCESS
alert('Your name is ' + response.name);
});
}
});
FB.api('/me', function(response) {
//FAILURE
alert('Your name is ' + response.name);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在调用 FB.* 或将 FB.* 附加到任何 JavaScript 处理程序之前,您必须确保框架已加载。
您可以通过将附件添加到 window.fbAsyncInit=function(){}; 内的处理程序来确保在正确的时间完成它
此外,您的第一个示例是正确的,您希望确保用户在盲目调用 FB.api( 或 FB.ui( 函数) 之前已连接/登录。
You have to be sure the framework is loaded before calling or attaching FB.* to any javascript handlers.
You can ensure you get it done at the right time by putting the attachment to handlers inside of the
window.fbAsyncInit=function(){};
Also, your first example is correct, you want to ensure the user is connected/logged in prior to just blindly calling
FB.api(
orFB.ui(
functions.