0

So I'm using CodeIgniter framework and trying to get user friends. I have no problems with authentification, but when I try to get user friends using GET method from https://graph.facebook.com/me/friends?fields=gender,id,name&access_token= it returns me empty array. This is the front end code

<script>
$(function () {
    FB.init({
        appId: ****, // App ID
        channelUrl: '', // Channel File
        status: true, // check login status
        cookie: true, // enable cookies to allow the server to access the session
        xfbml: true  // parse XFBML
    });
});

jQuery('*[data-action="doFbLogin"]').on('click', function () {
    FB.login(function (response) {
        if (response.authResponse) {

            FB.api('/me/friends', function(response) {

                // loading message
                var intervalLoading = 0;
                setInterval(function() {
                    intervalLoading = ++intervalLoading % 4;
                    jQuery('*[data-message="fb_loading"]').html("Connecting, please wait" + Array(intervalLoading + 1).join("."));
                }, 400);

                // request
                jQuery.post('http://www.****.***/ajax/fb_connect', {
                        "user_id": response.id,
                        "full_name": response.name,
                        "first_name": response.first_name,
                        "last_name": response.last_name,
                        "email": response.email,
                        "gender": response.gender,
                        "token": response.token,
                        "friends": response.friends,
                        "current_url": 'http://*****',
                        "ref_url": 'http://*****'
                    },
                    function (data) {
                        data = JSON.parse(data);
                        if (data == true) {
                            jQuery('*[data-message="fb_loading"]').removeAttr('data-message').html('Connected!');
                            location.reload();
                        } else {
                            jQuery('*[data-message="fb_loading"]').removeAttr('data-message').html('Connection failed!');
                            setTimeout(function(){
                                location.reload();
                            }, 2000);
                        }
                    }
                );
            });
        } else {
            console.log('something went wrong');
        }
    }, {scope: 'email'});
});

and this is my backend - connection:

$data = array(
        'fb-config' => array(
            'appId'  => FACEBOOK_API_ID,
            'secret' => FACEBOOK_SECRET
        )
    );

    $this->load->library('facebook', $data['fb-config']);
    $data['userData'] = $this->facebook->getUser();

    if ( $data['userData'] ) {
        try {
            $data['user_profile'] = $this->facebook->api('/me');
        } catch (FacebookApiException $e) {
            $data['user_profile'] = null; // return false, execution terminated.
            $this->facebook->destroySession();
        }
    }

$this->session->set_userdata(array('user_fb_token' => $this->facebook->getAccessToken()));
            print_r($this->users_model->getUserFacebookFriends());
            exit;

and method to get friends:

public function getUserFacebookFriends()
{
    // get facebook friends (crawl)
    $graph_link = 'https://graph.facebook.com/me/friends?fields=gender,id,name&access_token=';
    $user_friends = file_get_contents($graph_link . $this->session->userdata('user_fb_token'));

    $friendsArray = json_decode($user_friends, true);

    if ( is_array($friendsArray) ) {
        $data = array('friendsArray' => $friendsArray['data']);
    } else {
        $data = array('friendsArray' => FALSE);
    }

    return $data;
}

so I have no idea why this is not working, any suggestions?

Arnas Pečelis
  • 1,000
  • 2
  • 12
  • 32

2 Answers2

1

The result is correct. Since v2.0, you can only get friends who authorized your App too: https://developers.facebook.com/docs/apps/changelog

This is most likely the question that gets asked the most on stackoverflow, please use the search options (google, stackoverflow) before asking. A very detailed answer is in this thread, for example: Facebook Graph Api v2.0+ - /me/friends returns empty, or only friends who also use my app

Community
  • 1
  • 1
andyrandy
  • 72,880
  • 8
  • 113
  • 130
  • I saw all the answers but can't find right sollution. – Arnas Pečelis Feb 24 '15 at 22:16
  • there is no solution, you just can´t get all friends anymore. you can only get access to all friends for inviting or tagging only, which is explained in detail in that other stackoverflow thread. – andyrandy Feb 24 '15 at 22:49
0

I had the same issue. You can use FQL to get a list of friends. BUT FQL is deprecated and I think will be disabled in 2-3 months. With v2.0+, after privacy politics change, you can get only list of friends which already installed your app (they gave permissions to your app). In general, before v2.0 according to privacy, user was responsible for own account and friends accounts. After v2.0, user responsible only for own profile.

With v2.0 you can collect only list of friends to recommend them your app, so you have to change the way of promotion your app to collect friends profiles.

FQL documentation

The request looks like:

GET /fql?q=SELECT+uid2+FROM+friend+WHERE+uid1=me()&access_token=...

query can be modified of course, like this (you can add/remove fields and even add subquery):

SELECT+uid,+name,+first_name,+last_name,+pic_big,+birthday_date,+sex,+locale+FROM+user+WHERE+uid+IN+(SELECT+uid2+FROM+friend+WHERE+uid1=me())

BUT your account/application must be registered in 2014 year (I hope I'm correct, but you can check in documentation)

  • how to use it with FQL? – Arnas Pečelis Feb 24 '15 at 22:30
  • you can´t get all friends, not even with FQL. also, it is deprecated, but it will not be disabled in 2-3 months. you can use it in older apps a lot longer, but you can´t use it anymore in newer apps. it´s all in the changelog. – andyrandy Feb 24 '15 at 22:48
  • I didnt manage to collect all friends using new SDK for v2.0+ using old tokens (actually with new tokens I had the same issue). But code, which uses FQL, currently works fine and collects all friends. – Oleksii Chernomaz Feb 24 '15 at 22:56
  • @OleksiiChernomaz Then you get them with /me/friends also. And it only works because your app was created before 4/30/2014 and will stop working at 4/30/2015 – WizKid Feb 24 '15 at 23:14
  • is there is any options to change version of app I'm using now? becouse I can't use FQL of app version, response I get: `{ "error": { "message": "(#12) fql is deprecated for versions v2.1 and higher", "type": "OAuthException", "code": 12 } }` – Arnas Pečelis Feb 24 '15 at 23:47
  • I think, means that your appId is quite new and FQL is not available for you at all (OR maybe you are using a new version of SDK to obtain users accessToken, but I'm not sure that accessTokens are binded to SDK's version). Try to use what recommended @WizKid (prev comment) instead FQL in this case. But I assume, that it will not work for you. FB tries to make all new apps use new version of API – Oleksii Chernomaz Feb 25 '15 at 00:07