4

Below code gives me an 'undefined' on the name and email. However the user IS logged in because I DO have the access token and UID?

function getToken(){
    FB.login(function(response) {
        if (response.authResponse) {
            var access_token = response.authResponse.accessToken;
            var uid = response.authResponse.userID;
            document.getElementById('accessToken').value=access_token;
            document.getElementById('uid').value=uid;
            FB.api('/me', function(response) {
                console.log('Good to see you, ' + response.name + '.' +response.email);
            });
            document.forms['token'].submit();
        } else {
            return false;
        }
    }, {scope:'email,user_likes,friends_likes'});
}

When I output the complete response of fb.api to console I get

[11:46:10.369] ({error:"load-error: unknown"})
12
  • Can you try to just log the entire response and post it here so that we'll know what facebook respond with? Something like: console.log(response); Commented Apr 6, 2012 at 9:21
  • -- [11:46:10.369] ({error:"load-error: unknown"}) Commented Apr 6, 2012 at 9:48
  • First time I see this type of error. Can you maybe try a simpler flow? just call for login, then in the callback just check for the authResponse and if it exists call the FB.api with /me and then just log the response. Remove the other code, especially the form submitting. Then please post here the responses you get from fb for the login and /me requests. Commented Apr 6, 2012 at 9:59
  • Everything else in this script is working. I DO get the access token and DO get the userID Commented Apr 6, 2012 at 10:27
  • Yes, I understand that, but still, when things do go according to plan, it's usually best to simplify things in order to see what goes wrong and where. Just give it a go and we'll see what's the result. Commented Apr 6, 2012 at 10:43

2 Answers 2

1

I now see what you are trying to do and the problem.

Why it fails:

You are making an asynchronous request (using the FB.api method) and then submit a form (and it's a guess since you did not include all code) which reloads the entire page, messing up with the async request.

As for how to solve the problem:

The best solution, in my opinion, is to use the Server-Side authentication process and only then, afterwards, when the canvas loads use the client side sdk. That way you'll have access tokens both in the client and server side.

Another approach is not to submit the form, but to issue a ajax request to your server (it can be in POST) with the access token, since both the requests will be asynchronous (FB.api and the one to your server) everything should be a ok.

Sign up to request clarification or add additional context in comments.

Comments

0

I think a quick fix would be submitting the form inside the FB.api function. So it would look like:

FB.api('/me', function(response) {
            console.log('Good to see you, ' + response.name + '.' +response.email);
            document.forms['token'].submit();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.