FB.api error when fb.getLoginStatus works - javascript

very frustrated with javascript SDK for facebook.
can someone tell me why this works (gives an alert with my facebook name):
FB.getLoginStatus(function(response) {
if (response.authResponse) {
token = response.authResponse.accessToken;
FB.api('/me', function(response) {
console.log(response);
alert('Your name is ' + response.name);
// do something here they are logged in and have given you perms
});
} else {
// no user session available, someone you dont know
}
});
But this returns an error, "An active access token must be used to query information about the current user.":
FB.api('/me', function(response) {
console.log(response);
alert('Your name is ' + response.name);
});

But this returns an error, "An active access token must be used to query information about the current user."
Well, because you haven’t acquired an access token before calling the method …?
FB.getLoginStatus provides you with a fresh user access token, provided the user has authorized your app before.
But if you skip this step of interaction with Facebook – where do you expect a valid access token to come from?
Right, there is none – and therefore you are getting the error message, stating exactly that …

Related

Facebook Login only showing button

I am checking user login status.
I have used the facebook code that fb has given, but it shows nothing but the login button. I want to check if user is already logged in or not.
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML =
'<br/>Thanks for logging in, ' + response.name + '<br/>User Id: '+ response.id + '<br/>Email Id: '+ response.email + '!';
//window.location.replace('abc.html');
});
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
Your code is not the same as the facebook code I found published here. It is close but a couple keys differences and things to note. More of your file would be very helpful here. Please make sure that you have recieved and App ID from Facebook for your application and registered it. But to be clear, it seems like you are checking the login status at the wrong time. This is simply a function and we have no idea where it is called or if it mimicks the flow on the link I provided goes like this (even though this is not how they are organized within the code facebook has given). Also the only way you would see something is if you have a HTML text item with id='status'. All of this I cannot tell by your code.
Initialize FB SDK and do all proper setup
User presses login which executes this code:
<fb:login-button scope="public_profile,email" onlogin="checkLoginState();">
</fb:login-button>
The above code gives you the regular scope provided by facebook (to get any more scope you have to go through a request process from facebook, just FYI). As you can see once this button is pressed and the action described as onlogin is complete we will execute the function checkLoginState() which is:
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
This gets the logon "state" and then calls the function statusChangeCallback(response) :
function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(response);
// The response object is returned with a status field that lets the
// app know the current login status of the person.
// Full docs on the response object can be found in the documentation
// for FB.getLoginStatus().
if (response.status === 'connected') {
// Logged into your app and Facebook.
testAPI();
} else if (response.status === 'not_authorized') {
// The person is logged into Facebook, but not your app.
document.getElementById('status').innerHTML = 'Please log ' +
'into this app.';
} else {
// The person is not logged into Facebook, so we're not sure if
// they are logged into this app or not.
document.getElementById('status').innerHTML = 'Please log ' +
'into Facebook.';
}
}
Now the above code, IF CONNECTED, will call your testAPI() function which will pull down the response. And it will try to find an HTML document with the id status, if you didn't create this it won't display anything.
If you are sure that you did all these steps and did them correctly, then I recommend putting a couple console.log() statements and looking at the Google Chrome console or Javascript console in your browser and seeing what state you are in and that your functions are actually being called.
If you are not sure, please double check the Facebook Login page. It is good documentation.

About the facebook javascript sdk

I am a newbie in coding. When I called the below function to post on my wall. I always encouunter error occured alert box. I have search the web for few days still cannot find the result and the fix. The facebook documentation is really bad. Please help. I could call fb.ui, or fb.getloginstatus with no problem. But just have problem of calling the fb.api.
function postToWall() {
var body = 'Reading JS SDK documentation';
FB.api('/me/feed', 'post', { message: body }, function(response) {
if (!response || response.error) {
// alert('Error occured');
} else {
// alert('Post ID: ' + response.id);
}
});
FB.api('/me', function(response) {
// console.log(response);
});
}
The error message is-
An active access token must be used to query information about the current user.
The error is quite straight-forward. No user is currently logged-in to your application. You must call, FB.login first before making any API calls further.
After the login, the user will be in session and you wont see this error again.

How to get first and last name without access token?

According to the FB SDK docs, it appears that some data from the response object I should be able to retrieve sans access token. (https://developers.facebook.com/docs/reference/api/user/) However when I do this, I receive the message in the response object -
An active access token must be used to query information about the current user.
Does anyone have any insight on the matter?
Thanks a ton!
EDIT - the code I'm using:
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function (response) {
console.log('Good to see you, ' + response.name + '.');
console.log(response);
});
}
My finding so far indicate that basic information cannot be accessed unless one has either 'userid' or 'username' as in this post.
If one has to try and fetch basic information using following url, then access_token is needed
facebook
{
"error": {
"message": "An active access token must be used to query information about the current user.",
"type": "OAuthException",
"code": 2500
}
}

Why does my FB.api request always produce an error response?

I have a Facebook app with namespace 'thenovel'. It has an action 'rate' assigned to an object 'sentence'. When the 'dofacebookstuff()' function is called, I want the user's rating of a sentence - 'testsentence', for the moment - to be published to their timeline. 'Rating' doesn't require an access token.
Whenever the dofacebookstuff() is called, the error alert pops up. I have tested whether the '!response' or 'response.error' was causing it, and it is the 'response.error' every time.
I can get the user id, name etc. fine with FB.login but the api call just does not work. If I go straight to 'https://graph.facebook.com/me/thenovel:rate?sentence=testsentence&access_token=XXXXX', I see the following:
{
"data": [ ],
"paging": {
"next": "https://graph.facebook.com/me/thenovel:rate?sentence=testsentence&access_token=XXXXX&offset=25&limit=25"
}
}
I have checked my HTML and all the Facebook-related stuff is there and in the right place. None of the other scripts on the page produce errors. Here is the function in question:
function dofacebookstuff() {
FB.api(
'/me/thenovel:rate?sentence=hello&access_token=' + accesstoken,
'post',
function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Action ID: ' + response.id);
}
});
}
Could anyone explain where I'm going wrong? Where is my response data?
If you're posting to /me you need an active access token for that user - do you have one? it may have expired already, but i think the message is specifically referring to the fact that you haven't provided one, as there'd be a specific message if the token is invalid or expired.
You should be able to post to /USER_ID/thenovel:rate with the app access token if you're doing offline publishing of some kind
Try logging in using FB connect first.

FQL Javascript Access Token

I've pretty new to using FQL in any form, sorry if this is a simple question, but I can't seem to get around it.
I have this code that's attempting to get a Friend list:
var query = FB.Data.query('SELECT '+response.id+', '+response.name+' FROM friendlist from user where uid=' + response.id);
But the response I keep getting back is
"An active access token must be used to query information about the current user."
When I pull back the response object from the /me query I see all my information such as location, name, work, etc. But I don't see any access token
The code starts like this:
window.fbAsyncInit = function() {
FB.init({appId: '12...0', status: true, cookie: true, xfbml: true});
/* All the events registered */
FB.Event.subscribe('auth.login', function(response) {
// do something with response
login();
});
FB.Event.subscribe('auth.logout', function(response) {
// do something with response
logout();
});
FB.getLoginStatus(function(response) {
if (response.session) {
// logged in and connected user, someone you know
login();
}
});
How do I acquire an access token and provide along with the FQL query?
Thanks for any advice ahead of time!
The access token that you have to use in order to do all types of requests to the Graph API (including FQL queries) is given by facebook after the user has succesfuly logged in.
If you are using firebug do a console.log(response) after the user logs in to see the object that you receive.
FB.Event.subscribe('auth.login', function(response) {
console.log(response);
// do something with response
login();
});
It should contain 3 properties: perms, session and status. perms contains a list with permissions granted by the user, the status it's the status of the current user. What matters to you is the session property. This one it's also an object that among others has a access_token property (string) which you can use to do requests to API.
So, your query may look like this:
var query = FB.Data.query('SELECT '+response.id+', '+response.name+' FROM friendlist from user where uid=' + response.id + '&access_token='+response.session.access_token);
If you want to get the session in a better way, use FB.getSession, which returns the current session. More info here.
Good luck !

Categories