Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get friend list from Facebook using Android Facebook sdk? [closed]

I have created an app on facebook and achieve some first steps for implementing Facebook SDK.

This is what I'm trying to do: get list of friends from Facebook and to be able to pick some friends from that list and import them to my app.

How an I do that?

like image 535
Dusan Dimitrijevic Avatar asked Nov 11 '15 17:11

Dusan Dimitrijevic


People also ask

How do I get to my close friends list on Facebook?

It's beneath the person's name on their profile. Tap Edit Friend Lists. Select Close Friends. A blue check mark will appear next to the list's name.

Is Facebook SDK free?

The Graph API is free to use, for all applicable use cases.


1 Answers

Are you trying to get the Facebook friendlist of the user logged in into your app? Sounds like you need to perform a facebook graph request to acquire that list, and then withdraw the friends you want.
https://developers.facebook.com/docs/graph-api/reference/user/friendlists/

If you wanted to do it in Android java, her is an example:

    AccessToken token = AccessToken.getCurrentAccessToken();
        GraphRequest graphRequest = GraphRequest.newMeRequest(token, new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
                try {
                    JSONArray jsonArrayFriends = jsonObject.getJSONObject("friendlist").getJSONArray("data");
                    JSONObject friendlistObject = jsonArrayFriends.getJSONObject(0);
                    String friendListID = friendlistObject.getString("id"); 
                    myNewGraphReq(friendListID);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    Bundle param = new Bundle();
    param.putString("fields", "friendlist", "members");
    graphRequest.setParameters(param);
    graphRequest.executeAsync();

Since "member" is an edge in "friendlist" you can do a new request with your friendlist Id to get the members of that particular friend list. https://developers.facebook.com/docs/graph-api/reference/friend-list/members/

private void myNewGraphReq(String friendlistId) {
    final String graphPath = "/"+friendlistId+"/members/";
    AccessToken token = AccessToken.getCurrentAccessToken();
    GraphRequest request = new GraphRequest(token, graphPath, null, HttpMethod.GET, new GraphRequest.Callback() {
        @Override
        public void onCompleted(GraphResponse graphResponse) {
            JSONObject object = graphResponse.getJSONObject();
            try {
                JSONArray arrayOfUsersInFriendList= object.getJSONArray("data");  
                /* Do something with the user list */
                /* ex: get first user in list, "name" */
                JSONObject user = arrayOfUsersInFriendList.getJSONObject(0);
                String usersName = user.getString("name");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });
     Bundle param = new Bundle();
    param.putString("fields", "name");
    request.setParameters(param);
    request.executeAsync();
}

In the documentation for Facebook Graph request you can see what you can do with the User objects. I don't have enough rep to post another link unfortunately.

Keep in mind that a user must have signed in with facebook to get the access token needed to do these operations.

Well, hope it was something remotely like this you were looking for.

Edit: this method does not work anymore, the first link about friendlists shows that it is deprecated since 4 April 2018.

like image 184
Slagathor Avatar answered Oct 04 '22 15:10

Slagathor