1

我正在尝试使用 facebook graph api 获取测试用户的朋友,但它只返回朋友的 id,而不返回其他详细信息,如 first_name、last_name 等。这适用于真实用户。我专门传递了我需要的字段(请求看起来像这样):

https://graph.facebook.com/me/friends?access_token=...&fields=id%2Cfirst_name%2Clast_name%2Cgender%2Clocale%2Clink%2Clocation%2Cbirthday%2Creligion%2Crelationship_status

访问令牌是测试用户登录我的应用程序时授予测试用户的令牌。

我找到了类似的 SO 帖子,但我不确定建议的解决方案是什么。

通过 Facebook Graph API 查询测试用户

4

1 回答 1

2

您需要使用该应用程序access_token,这可以通过执行以下操作在 Graph API Explorer 中进行测试:

  1. 转到访问令牌工具并复制您的应用程序access_token
  2. 转到Graph API Explorer并从下拉列表中选择您的应用程序。
  3. 查询APP_ID/accounts/test-users?access_token=app_access_token这将检索测试用户
  4. 单击其中一个 ID(确保该用户与至少一个其他测试用户是朋友)并将查询更改为如下所示:TEST_USER_ID/friends?fields=first_name,last_name,gender&access_token=app_access_token
  5. 你完成了!:-)

更新
根据下面的评论,我尝试检索测试用户朋友的生日,但结果不一致。

  • 使用测试用户的access_token检索了 ID、生日和性别,但没有检索到姓名
  • 使用应用程序access_token检索了 ID、姓名和性别,但未检索生日

这是一个简单的代码:

<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head></head>
<body>
<div id="fb-root"></div>
<script>
// You can retrieve this from: https://developers.facebook.com/tools/access_token/
var app_access_token = "APP_ACCESS_TOKEN";
  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'APP_ID_HERE', // App ID
      channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      oauth      : true, // enable OAuth 2.0
      xfbml      : true  // parse XFBML
    });

    // Additional initialization code here
  };

  // Load the SDK Asynchronously
  (function(d){
     var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     d.getElementsByTagName('head')[0].appendChild(js);
   }(document));
</script>

<table id="friends">
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Gender</th>
        <th>Birthday</th>
    </tr>
</table>

<script>
function login(app) {
    FB.login(function(response) {
        if (response.authResponse) {
            var obj = {fields: 'name,gender,birthday', limit: 500};
            if(app)
                obj.access_token = app_access_token;
            FB.api('/'+response.authResponse.userID+'/friends', 'GET', obj, function(response) {
                console.log(response);
                if(response && response.data.length) {
                    var html = '';
                    for(var i=0; i<response.data.length; i++) {
                        html += "<tr>";
                        html += "<td>" + response.data[i].id + "</td>";
                        html += "<td>" + response.data[i].name + "</td>";
                        html += "<td>" + response.data[i].gender + "</td>";
                        html += "<td>" + response.data[i].birthday + "</td>";
                        html += "</tr>";
                    }
                    document.getElementById("friends").innerHTML += html;
                }
            });
        } else {
            console.log('User cancelled login or did not fully authorize.');
        }
    }, {scope: 'user_birthday,friends_birthday'});

}
</script>
<button onclick="login();">List Friends With User Token</button>
<button onclick="login(1);">List Freinds With App Token</button>
</body>
</html>

使用我的一个测试用户帐户的结果:
在此处输入图像描述
前三行使用“ List Friends With User Token ”检索,其余行使用“ List Freinds With App Token ”检索。

最重要的是,这需要 Facebook 关注!

于 2011-11-23T18:06:22.070 回答