Search code examples
facebook-graph-apioauthfacebook-oauth

Accessing Test User Data with facebook graph api


I am trying to get the friends of a test user using facebook graph api but it returns only the ids of the friends but not other details like first_name, last_name etc. This works though with real users. I specifically pass the fields that I require (the request looks something like this):

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

The access token is the token granted to the test user when the test user logs in to my application.

I have found a similar SO post but I am not sure what is the solution suggested.

Query test users through Facebook Graph API


Solution

  • You need to use the App access_token and this can be tested in the Graph API Explorer by doing the following:

    1. Go to the Access Token tool and copy your app access_token
    2. Go to Graph API Explorer and choose your app from the dropdown list.
    3. Query APP_ID/accounts/test-users?access_token=app_access_token this would retrieve the test users
    4. click on one of the ids (make sure this user is friend with at least one other test user) and change the query to look like: TEST_USER_ID/friends?fields=first_name,last_name,gender&access_token=app_access_token
    5. You are done! :-)

    UPDATE:
    Based on the comments below, I've tried to retrieve the birthday of the test user's friends but the results were inconsistent.

    • Using test user's access_token: IDs, birthdays and gender were retrieved but not names
    • Using App access_token: IDs, names and gender were retrieved but not birthdays

    Here's a simple code:

    <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>
    

    The result using one of my test user accounts:
    enter image description here
    The first three rows were retrieved with "List Friends With User Token" and the rest with "List Freinds With App Token".

    Bottom line, this needs Facebook attention!