简体   繁体   English

Fb.api未定义的用户

[英]Fb.api undefined user

I have the following problem: 我有以下问题:

Once the user is logged in facebook if I run the following code: 用户登录Facebook后,如果我运行以下代码:

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

It pops up an alert with "Udefined" written. 它弹出一个带有“ Udefined”字样的警报。

But if I change the code in the following way: 但是,如果我以以下方式更改代码:

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

It response in the correct manner. 它以正确的方式响应。

How it is possible ? 怎么可能? Why '/me' never works ? 为什么“ /我”永远不起作用?

Use the below code to overcome 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);
            });
        } 
    });

I think you might be querying "/me" in context of the app rather than the user. 我认为您可能是在应用而非用户的上下文中查询“ / me”。 The documentation is not as explicit as it could be, but I've had the same issue. 该文档没有尽可能明确,但是我遇到了同样的问题。

Are you able to use FB.getLoginStatus ? 您可以使用FB.getLoginStatus吗?

After I have FB.init set up, I make a call to FB.getLoginStatus similar to what is found below. 设置FB.init后,将调用FB.getLoginStatus,类似于下面的内容。 There might be a better way on how to do this, but it works for me. 可能会有更好的方法来执行此操作,但这对我有用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM