3

I saw lots of questions like this in Stack Overflow but they didn't solve my problem. I referred this, this and also Documentation links like this and this


I used LoginButton to login in my App.

I am able to get user's (which is logged in) Name, Email, etc,using following code (it works fine):

private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken = loginResult.getAccessToken();
            Profile profile = Profile.getCurrentProfile();
                    GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(
                                JSONObject object,
                                GraphResponse response) {
                            // Application code
                            try {

                                email = object.getString("email");
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            Log.e("EMAIL",email);
                            Log.e("GraphResponse", "-------------" + response.toString());
                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,link,gender,birthday,email");
            request.setParameters(parameters);
            request.executeAsync();
}

with permissions:

loginButton.setReadPermissions("user_friends");
loginButton.registerCallback(callbackManager, callback);

I got JSON in LogCat. But now I want to get Friends list, So I wrote code by seeing Documentation and changed my code slightly as follows:

private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken = loginResult.getAccessToken();
            Profile profile = Profile.getCurrentProfile();
            // I saw following code in Documentation 
            new GraphRequest(
                    AccessToken.getCurrentAccessToken(),
                    "/{friendlist-id}",   /* I actually tried ,friend-list-id' , /me/friends' , '/me/taggable_Friends' and many*/
                    null,
                    HttpMethod.GET,
                    new GraphRequest.Callback() {
                        public void onCompleted(GraphResponse response) {
         //    handle the result
                            Log.d("RESPONSE KBT",response.toString());
                        }
                    }
            ).executeAsync();

        }

with permissions:

loginButton.setReadPermissions(Arrays.asList("email", "user_friends","read_custom_friendlists"));
loginButton.registerCallback(callbackManager, callback);

I get this in my LogCat:

RESPONSE KBT﹕ {Response:  responseCode: 404, graphObject: null, error: {HttpStatus: 404, errorCode: 803, errorType: OAuthException, errorMessage: (#803) Some of the aliases you requested do not exist: {friendlist-id}}}
Community
  • 1
  • 1
Ganesh
  • 1,820
  • 2
  • 20
  • 40
  • 1
    You can get only 2 "type" of friends: TaggableFriends or InvitableFriends (for game app) if you would taggableFriends I have the code.. – Michele Lacorte Dec 06 '15 at 12:11
  • @Michele Lacorte : I tried taggablefriends too, i got the same logcat. It will be helpful if you share your code. (Anyhow this is not game app , This is a simple app just to show friends list and i am actually doing this for education purpose) – Ganesh Dec 06 '15 at 12:22
  • do you want to tag friends? if not, don´t use taggable_friends. – andyrandy Dec 06 '15 at 12:35
  • @luschn I just want to get list of friends of logged in user – Ganesh Dec 06 '15 at 12:52
  • ok, that´s not possible. see my answer. everything you need to know is in there. – andyrandy Dec 06 '15 at 12:53

3 Answers3

4

Try this:

Permission:

setReadPermissions(Arrays.asList("public_profile", "email", "user_friends"));

This variable for process your friends:

public static List<TaggableFriends> friendListFacebook = new ArrayList<TaggableFriends>();

This method for get friends (call it in onSuccess() method in your login class):

public void getFriends()
{
    if(AccessToken.getCurrentAccessToken() != null)
    {
        GraphRequest graphRequest = GraphRequest.newGraphPathRequest(
                AccessToken.getCurrentAccessToken(),
                "me/taggable_friends",
                new GraphRequest.Callback()
                {
                    @Override
                    public void onCompleted(GraphResponse graphResponse)
                    {
                        if(graphResponse != null)
                        {
                            JSONObject jsonObject = graphResponse.getJSONObject();
                            String taggableFriendsJson = jsonObject.toString();
                            Gson gson = new Gson();
                            TaggableFriendsWrapper taggableFriendsWrapper= gson.fromJson(taggableFriendsJson, TaggableFriendsWrapper.class);
                            ArrayList<TaggableFriends> invitableFriends = new ArrayList<TaggableFriends>();
                            invitableFriends = taggableFriendsWrapper.getData();
                            int i;
                            for(i = 0; i < invitableFriends.size(); i++)
                            {
                                try
                                {
                                    friendListFacebook.add(invitableFriends.get(i));
                                }
                                catch(Exception e){}
                            }
                        }else {

                        }

                    }
                }
        );

        Bundle parameters = new Bundle();
        parameters.putInt("limit", 5000); //5000 is maximum number of friends you can have on Facebook

        graphRequest.setParameters(parameters);
        graphRequest.executeAsync();
    }
}

Add this class:

TaggableFriendsWrapper:

public class TaggableFriendsWrapper {

private ArrayList<TaggableFriends> data;
private Paging paging;

public ArrayList<TaggableFriends> getData() {
return data;
}

public void setData(ArrayList<TaggableFriends> data) {
this.data = data;
}

public Paging getPaging() {
return paging;
}

public void setPaging(Paging paging) {
this.paging = paging;
}

public class Paging {

private Cursors cursors;
public Cursors getCursors() {
    return cursors;
}

public void setCursors(Cursors cursors) {
    this.cursors = cursors;
}
}

public class Cursors {
private String after;
private String before;

public String getAfter() {
    return after;
}
public void setAfter(String after) {
    this.after = after;
}
public String getBefore() {
    return before;
}
public void setBefore(String before) {
    this.before = before;
}

}
}

TaggableFriends:

public class TaggableFriends {

private String id;
private String name;
private Picture picture;

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public Picture getPicture() {
return picture;
}
public void setPicture(Picture picture) {
this.picture = picture;
}

public class Picture {

private Data data;

public Data getData() {
    return data;
}

public void setData(Data data) {
    this.data = data;
}
}

public class Data {

private String url;
private boolean is_sillhouette;

public String getUrl() {
    return url;
}
public void setUrl(String url) {
    this.url = url;
}
public boolean isIs_sillhouette() {
    return is_sillhouette;
}
public void setIs_sillhouette(boolean is_sillhouette) {
    this.is_sillhouette = is_sillhouette;
}
}
}
Michele Lacorte
  • 5,323
  • 7
  • 32
  • 54
  • It is com.google.gson.Gson class and it look like Json – Michele Lacorte Dec 06 '15 at 12:40
  • Its saying incompatible error at this line --> invitableFriends = taggableFriendsWrapper.getData(); <-- And another error is --> cannot resolve symbol 'friendListFacebook' <-- on next line – Ganesh Dec 06 '15 at 12:45
  • you have to add friendListFacebook (I wrote it) in your class, and import TaggableFriends and TaggableFriendsWrapper... the code works, I assure you! – Michele Lacorte Dec 06 '15 at 12:58
  • he does not want to tag friends, so he can´t use taggable_friends – andyrandy Dec 06 '15 at 13:05
  • @MicheleLacorte : Okay.. what about this " incompatible error at this line --> invitableFriends = taggableFriendsWrapper.getData(); " – Ganesh Dec 06 '15 at 13:57
  • @luschn : I don't want to tag friends, but i need to get friends list in any way ( it may be taggable_friends or anything ) – Ganesh Dec 06 '15 at 13:59
  • i can only repeat myself (and my answer): you can´t get all your friends. you can ONLY get all your friends if you want to tag or invite them (to a game). and you will only get a tagging or inviting token with those endpoints. – andyrandy Dec 06 '15 at 14:00
  • btw, that coding is missing taggable and invitable friends. and why user_location in the scope? – andyrandy Dec 06 '15 at 14:02
  • @Ganesh so...? what's your problem? – Michele Lacorte Dec 08 '15 at 09:31
  • My aim is just to get friends's names and list it in a recycler view.... But luschn said its not possible for privacy reasons.... So i am accepting that as answer and I am. assuming my aim is an impossible thing to do. – Ganesh Dec 08 '15 at 09:38
  • Exactly, but , using this code , all in all you have most of the friends ... for the moment I see no other alternative – Michele Lacorte Dec 08 '15 at 09:39
2

The correct API endpoint to get the friends of the authorized user is /me/friends and you need to authorize with the user_friends permission. Keep in mind that you will only get friends who authorized your App with user_friends too.

You can ONLY get access to ALL friends for tagging (with /me/taggable_friends) or inviting friends to a game with Canvas implementation (with /me/invitable_friends).

More information: 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
  • Do you mean it is not at all possible to get logged-in user's friend list ? I don't want just friends who are using my app. I need all the friends, is there any way to do that? – Ganesh Dec 06 '15 at 12:58
  • 1
    no, there is no way, for privacy reasons. only friends who authorized your app too show up with /me/friends. – andyrandy Dec 06 '15 at 13:00
  • I used taggable frns end point... AND it gave me some friends names... well thank you for clearing my doubt – Ganesh Dec 08 '15 at 09:34
  • not sure if that was not clear enough, but you are not allowed to use taggable_friends for anything else than tagging. you will find out when you go through the review process ;) – andyrandy Dec 08 '15 at 10:40
  • :Man you actually cleared my doubt... i was searching for 2 days to get friend list. If you didn't said, I would have wasted another 2 more days. Thanks a lot. I have lots of doubts in android as I am a beginner. I am gonna post few more questions. If you can help me please give me your email ID so that I can share my questions with you. Promise I won't disturb you! :) Hope you ll help me to grow bigger in Android development. – Ganesh Dec 08 '15 at 10:44
  • no worries, i am on stackoverflow almost every day, so i will see your questions ;) – andyrandy Dec 08 '15 at 10:49
  • I really need an option in stack overflow to bookmark someone and able to share our questions to another stackoverflow member... :-( – Ganesh Dec 08 '15 at 10:52
  • but you know, people don´t like to get notified or dragged into a question. not a good idea to tag someone or tell them about every new question. people will see it anyway if they are active ;) – andyrandy Dec 08 '15 at 11:22
0

Try doing without Brackets {}

new GraphRequest(
                    AccessToken.getCurrentAccessToken(),
                    "/me/friendlist-id",   /* I actually tried ,friend-list-id' , /me/friends' , '/me/taggable_Friends' and many*/
                    null, ...... );