【问题标题】:Accessing Test User Data with facebook graph api使用 facebook graph api 访问测试用户数据
【发布时间】:2012-01-04 23:29:18
【问题描述】:

我正在尝试使用 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 帖子,但我不确定建议的解决方案是什么。

Query test users through Facebook Graph API

【问题讨论】:

    标签: facebook-graph-api oauth facebook-oauth


    【解决方案1】:

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

    1. 转到Access Token tool 并复制您的应用程序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. 你完成了! :-)

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

    • 使用测试用户的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 关注!

    【讨论】:

    • 使用上面的方法,我可以得到朋友的名字和姓氏,但不能得到他们的生日。另外不清楚为什么一定要用appAccessToken来获取好友的详细信息?
    • 酷。感谢更新。由于这是一个 facebook 问题,我不确定我是否真的可以将此答案标记为已接受的答案,但无论如何都要这样做,因为除了向 facebook 报告之外,没有什么可以做的了。
    • @DivKis01,你应该把它作为一个错误报告给 Facebook(链接到这个问题),如果你在这里得到一个 official 答案“不接受”我的答案并接受官方
    • 向 Facebook 报告的错误记录在这里:developers.facebook.com/bugs/329960593684545
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多