Ad

Get List Of Facebook Friends That Have The App Installed

I'm making my app and I need to get some info from the Facebook user, I have the gender and the locale but I dont know how to get the friends that have the app installed. Here is my code so far:

public class FacebookUser {
private String gender, locale;
private FirebaseUser user;
private FirebaseAuth auth;
private Context context;



public FacebookUser(final Context context){
    gender=null;
    locale=null;
    this.context=context;
    auth=FirebaseAuth.getInstance();
    user=auth.getCurrentUser();

}
public void createUser(){
    FacebookSdk.sdkInitialize(context);
    GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
            new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject object, GraphResponse response) {
                    try{
                        Log.e("RESPONSE: ",response.getRawResponse());
                        gender = object.getString("gender");
                        locale = object.getString("locale");
                        User currentUser = new User(user.getDisplayName(), user.getEmail(),locale, gender, null,User.UNDEFINED, CuboRubik.UNDEFINED, 0, 0 ,0.00000001F,context);
                        User.putUserInShared(currentUser, context);
                        AddToDatabase addToDatabase = new AddToDatabase(context);
                        addToDatabase.createUser(currentUser, context);
                    }catch(JSONException e){
                        e.printStackTrace();
                    }
                }
            });
    Bundle bundle = new Bundle();
    bundle.putString("fields", "locale,gender");
    request.setParameters(bundle);
    request.executeAsync();
}

And here is a try for getting the list of friends...

 private void friendGraphRequest(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");
                Log.e("USERNAME: ",""+usersName);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });
    Bundle param = new Bundle();
    param.putString("fields", "name");
    request.setParameters(param);
    request.executeAsync();
}

I know there are somo things wrong but maybe I can use some of that code. Thanks!

Ad

Answer

In the friendGraphRequest code, you're accessing friendListId/members which is part of an old API for accessing 'Friend lists' (used by Facebook users to organise their friends into different lists) - this probably isn't what you want

To access a user's list of friends, the API call is /<USER>/friends (or /me/friends as shorthand)

This returns the name and ID of the user's friends who also use your app, plus a count of all the user's friends (in the summary key in the response)

If you want to keep the code you already have, set graphPath to /me/friends and work from there

Ad
source: stackoverflow.com
Ad