1

Facebookグラフ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投稿を見つけましたが、提案されている解決策がわかりません。

FacebookGraphAPIを介してテストユーザーにクエリを実行します

4

1 に答える 1

2

アプリを使用する必要があります。access_tokenこれは、次の手順を実行することでGraphAPIExplorerでテストできます。

  1. アクセストークンツールに移動し、アプリをコピーしますaccess_token
  2. Graph API Explorerに移動し、ドロップダウンリストからアプリを選択します。
  3. これをクエリAPP_ID/accounts/test-users?access_token=app_access_tokenすると、テストユーザーが取得されます
  4. IDの1つをクリックし(このユーザーが少なくとも1人の他のテストユーザーと友達であることを確認してください)、クエリを次のように変更します。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>

テストユーザーアカウントの1つを使用した結果:
ここに画像の説明を入力してください
最初の3行は「ユーザートークンで友達を一覧表示」で取得され、残りは「アプリトークンで友達を一覧表示」で取得されました。

結論として、これにはFacebookの注意が必要です。

于 2011-11-23T18:06:22.070 に答える