EboMike
EboMike

Reputation: 77762

How to get the full friends list using the Android Facebook SDK

I'm trying to get a list of all friends using the Facebook Android SDK 3.23.0.

I only get the first 20 results, and when trying to go to the next page, I just get an empty result.

Here's the code:

    Request.GraphUserListCallback callback = new Request.GraphUserListCallback() {
        @Override
        public void onCompleted(List<GraphUser> graphUsers, Response response) {
            parseFriends(graphUsers);
        }
    };

    Bundle params = new Bundle();
    params.putString("fields", "id, name");
    Request request = Request.newMyFriendsRequest(mSession, callback);

    while (request != null) {
        request.setParameters(params);

        Response response = request.executeAndWait();

        if (response.getError() != null) {
            Log.e(TAG, "Error in request: " + response.getError().toString());
            break;
        } else {
            request = response.getRequestForPagedResults(Response.PagingDirection.NEXT);
        }
    }

That doesn't work, the second request (the one created by getRequestForPagedResults()) is essentially empty (the graph path is "null").

When I look at the response of the first request, the "next" tag seems perfectly fine:

https://graph.facebook.com/v2.2/625641456/friends?fields=id,name&format=json&access_token=[TOKEN]&limit=25&offset=25&__after_id=[ID]

(I do wonder why the offset is 25 when the first request only returned 20 results. I'm using the default limit, which I thought is 25).

My backup plan was to keep track of the number of elements I process and simply add "offset" into the bundle that I pass in. But that results in the second request being empty, and its "next" being null.

So I tried parsing the resulting "next" string from the first request and passing all its contents into the bundle, i.e. _after_id, offset, limit, etc... but with the same result - empty result, and no "next".

How do I properly retrieve the full friends list?

Upvotes: 2

Views: 1395

Answers (3)

hsafarya
hsafarya

Reputation: 1053

If you change "v2.2" to "v1.0" , then you will get what you want, but be aware 1.0 api will expire soon.

Upvotes: 0

EboMike
EboMike

Reputation: 77762

Simple answer: It's not possible. Facebook removed the option to retrieve friend data. The only exception is from friends who are using the same app, so out of all my friends, 20 are apparently using my app, and those are the people I got back from my query.

Outside that, you can't get the full friends list anymore, or details about those friends.

Upvotes: 4

Bogdan V.
Bogdan V.

Reputation: 769

Try to set the limit from 25 to no-limit (or very big limit) and let the rest of the code as it is. I worked with the SDK in older versions, and it worked in peculiar ways sometimes (but i tend to remember that i was getting the friend list in full, regardless of its size).

Also, try to move the request.setParams() outside the while. If there is any interim state of the request, you just overwrite them. Facebook may reject identical requests that come too quick one after another to mitigate DoS attacks.

Upvotes: 1

Related Questions