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