User Fb.api undefined

I have the following problem:

After user login to facebook, if I run the following code:

FB.api('/me', function(user) { alert(user.name); }); 

A warning appears that says "Udefined".

But if I changed the code as follows:

 FB.api( /[MY_REAL_FACEBOOK_ID], function(user) { alert(user.name); }); 

The answer will be correct.

How is this possible? Why does '/ me' never work?

+4
source share
2 answers

I think you could request "/ me" in the context of the application, and not for the user. The documentation is not as clear as it could, but I had the same problem.

Can you use FB.getLoginStatus ?

After installing FB.init, I make a call to FB.getLoginStatus, similar to the one below. Perhaps the best way to do this, but it works for me.

Hope this helps:

  FB.getLoginStatus(function (response) { if (response.session) { // logged in and connected user, someone you know graphUrl = "https://graph.facebook.com/me?access_token=" + response.session.access_token + "&callback=displayUser" var script = document.createElement("script"); script.src = graphUrl; document.body.appendChild(script); } else { // no user session available, someone you dont know }}); function displayUser(user) { alert(user.name); } 
+1
source

Use the code below to solve the Undefined problem:

 window.fbAsyncInit = function() { // init the FB JS SDK FB.init({ appId : your_app_id, status : true, xfbml : true }); // Additional initialization code such as adding Event Listeners goes here FB.getLoginStatus(function(response) { if (response.status === 'connected') { alert("connected"); connecter=true; FB.api('/me', function(user) { alert(user.name); alert(user.first_name); alert(user.last_name); alert(user.email); }); } }); 
+1
source

Source: https://habr.com/ru/post/1343628/


All Articles