Javascript 正在尝试获取response.name或id

Javascript 正在尝试获取response.name或id,javascript,facebook,Javascript,Facebook,在我的页面上,人们可以按下共享按钮并在Facebook上发布。 我使用以下代码: window.fbAsyncInit = function() { FB.init( { "appId" : "<?php echo $fbid; ?>", "status" : true, "cookie" : true, "xfbml" : true, "oauth" : true });

在我的页面上,人们可以按下共享按钮并在Facebook上发布。 我使用以下代码:

window.fbAsyncInit = function() {
    FB.init(
    {
        "appId"  : "<?php echo $fbid; ?>",
        "status" : true,
        "cookie" : true,
        "xfbml"  : true,
        "oauth"  : true
    });
在我使用以下代码的地方,我想显示执行Sahre的人的用户名,但它没有:(

我如何才能获得进行共享的个人的用户名或用户ID? 它将打开警报框,内容为“未定义”。 谢谢你的帮助

我试图得到回复。状态和奇怪的事情是:即使我通过我的应用程序连接并在Facebook上发帖,我也会通过我的应用程序收到我没有连接的消息。使用该代码:

    if (response.status === 'connected') {
    // the user is logged in and has authenticated your app
    alert('All good');
    }else if (response.status === 'not_authorized') {
   // the user is logged in but NOT through your app
   alert('Not good');
   }else{
    alert('What the heck?');// the user isn't logged in to Facebook.
  }
 }); 

尼尔

我看到两件我觉得不好的事情:

  • FB.api
    调用包含在
    FB.ui
  • 您使用
    response
    两次:
    • FB.ui({…},函数(响应){…
    • FB.api('/me',函数(响应){…
这两个问题可能有关联,也可能没有关联。但这会导致:

function showUserName() {
    FB.api('/me', function(response) {
        alert('Post was published by ' + response.name);
    });
}
function showStreamPublish() {
    FB.ui({
        method: 'feed',
        name: 'Text',
        caption: 'Text',
        link: 'Link',
        picture: 'Pic',
        description: 'ni',
        user_message_prompt: 'Share it!'
    }, function(response) {
        if (response && response.post_id) {
            showUserName();
        } else {
            alert('Post was not published.');
        }
    }
);

到目前为止,可能是Thx的复制品。但即使在这里,我也得到了“未定义发布的帖子”。我没有使用更多的代码,Facebook上的帖子也成功了。这很奇怪,仍然很有趣:帖子是由undefined发布的。我不知道为什么!应用程序可以访问用户数据,用户已连接,帖子成功了!你确定这是一个真实的用户帐户,而不是一个没有名字的商业帐户吗?如果是一个商业帐户,那么当用户登录、无法管理应用程序或联系其他用户等时,帐户将始终被带到他们的页面列表中(或者您从未实际登录过该用户,但我假设您已登录,并且该用户ID存在于代码的其他位置)如果用户已经连接到facebook,我会在帖子完成后获得用户名,但如果他必须通过我的应用程序连接,我会获得“未定义”的用户名。
    if (response.status === 'connected') {
    // the user is logged in and has authenticated your app
    alert('All good');
    }else if (response.status === 'not_authorized') {
   // the user is logged in but NOT through your app
   alert('Not good');
   }else{
    alert('What the heck?');// the user isn't logged in to Facebook.
  }
 }); 
function showUserName() {
    FB.api('/me', function(response) {
        alert('Post was published by ' + response.name);
    });
}
function showStreamPublish() {
    FB.ui({
        method: 'feed',
        name: 'Text',
        caption: 'Text',
        link: 'Link',
        picture: 'Pic',
        description: 'ni',
        user_message_prompt: 'Share it!'
    }, function(response) {
        if (response && response.post_id) {
            showUserName();
        } else {
            alert('Post was not published.');
        }
    }
);