如何从Facebook获取好友列表?

3
我在Stack Overflow上看到了很多类似的问题,但它们没有解决我的问题。我参考了这个这个以及文档链接,如这个这个

我在我的应用中使用LoginButton登录。

我能够使用以下代码获取已登录用户的名称、电子邮件等信息(它可以正常工作):

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();
}

涉及权限:

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

我在日志中得到了 JSON。但现在我想要获取 好友列表,因此我查看了 文档 并稍微修改了一下我的代码:

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();

        }

带有权限:

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

我在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}}}

1
你只能获得两种类型的好友:可标记好友或可邀请好友(适用于游戏应用)。如果你需要可标记好友,我有代码。 - Michele Lacorte
@Michele Lacorte:我也尝试了taggablefriends,但是我得到了相同的logcat。如果您能分享您的代码,那将会很有帮助。(无论如何,这不是游戏应用程序,这只是一个简单的应用程序,用于展示朋友列表,我实际上是为了教育目的而这样做的) - Ganesh
你想标记朋友吗?如果不想,请不要使用taggable_friends。 - andyrandy
@luschn 我只想获取已登录用户的好友列表。 - Ganesh
好的,那是不可能的。请看我的回答,你所需要知道的一切都在里面了。 - andyrandy
3个回答

4
尝试这个: 权限:
setReadPermissions(Arrays.asList("public_profile", "email", "user_friends"));

这个变量用于处理你的朋友:
public static List<TaggableFriends> friendListFacebook = new ArrayList<TaggableFriends>();

获取好友列表的方法(在登录类中的 onSuccess() 方法中调用):

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;
}

}
}

可标记好友:

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;
}
}
}

这是 com.google.gson.Gson 类,看起来像是 Json。 - Michele Lacorte
它在这一行显示不兼容错误 --> invitableFriends = taggableFriendsWrapper.getData(); <-- 另一个错误是 --> 无法解析符号 'friendListFacebook' <-- 在下一行 - Ganesh
@Ganesh 那又怎样?你有什么问题吗? - Michele Lacorte
我的目的只是获取朋友的姓名并将其列在一个循环视图中... 但是luschn说出于隐私原因这是不可能的... 所以我接受那个答案,并假设我的目标是不可能实现的。 - Ganesh
没错,但是使用这段代码,总的来说你拥有了大部分的朋友...目前我看不到其他的选择。 - Michele Lacorte
显示剩余6条评论

2

1
不,出于隐私原因,没有办法。只有授权您的应用程序的朋友才能显示在/me/friends中。 - andyrandy
我使用了可标记的好友终端点...然后它给了我一些好友的名字...好的,谢谢你解决我的疑惑。 - Ganesh
没问题,我几乎每天都会上 StackOverflow,所以我会看到你的问题的 ;) - andyrandy
我真的很需要在Stack Overflow中添加一个收藏夹选项,以便能够将某人加入收藏并与其他Stack Overflow成员分享我们的问题... :-( - Ganesh
但是你知道,人们不喜欢被通知或卷入问题中。标记某人或告诉他们每个新问题都不是一个好主意。如果他们活跃的话,人们会自己看到的 ;) - andyrandy
显示剩余3条评论

0
尝试不使用花括号{}。
new GraphRequest(
                    AccessToken.getCurrentAccessToken(),
                    "/me/friendlist-id",   /* I actually tried ,friend-list-id' , /me/friends' , '/me/taggable_Friends' and many*/
                    null, ...... );

这是一个实际的ID,没有名为/me/friendlist-id的端点。 - andyrandy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接