如何使用 Android Facebook sdk 从 Facebook 获取好友列表? [英] How to get friend list from Facebook using Android Facebook sdk?

查看:35
本文介绍了如何使用 Android Facebook sdk 从 Facebook 获取好友列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 facebook 上创建了一个应用程序,并完成了一些实施 Facebook SDK 的第一步.

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

这就是我想要做的:从 Facebook 获取朋友列表,并能够从该列表中挑选一些朋友并将他们导入我的应用程序.

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.

我该怎么做?

推荐答案

您是否正在尝试获取登录到您的应用的用户的 Facebook 好友列表?听起来您需要执行 facebook 图形请求来获取该列表,然后撤消您想要的朋友.
https://developers.facebook.com/docs/graph-api/参考/用户/好友列表/

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/

如果你想用Android java来做,她就是一个例子:

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

由于成员"是好友列表"中的一个优势,您可以使用好友列表 ID 执行新请求以获取该特定好友列表的成员.https://developers.facebook.com/docs/graph-api/reference/friend-list/members/

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

在 Facebook Graph 请求的文档中,您可以看到可以对 User 对象执行的操作.不幸的是,我没有足够的代表来发布另一个链接.

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.

请记住,用户必须已登录 Facebook 才能获取执行这些操作所需的访问令牌.

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.

此方法不再有效,关于好友列表的第一个链接显示自 2018 年 4 月 4 日起已弃用.

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

这篇关于如何使用 Android Facebook sdk 从 Facebook 获取好友列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆