简体   繁体   中英

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, ie _after_id, offset, limit, etc... but with the same result - empty result, and no "next".

How do I properly retrieve the full friends list?

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.

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.

如果将“ v2.2”更改为“ v1.0”,则将获得所需的内容,但是请注意1.0 api将很快过期。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM