且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Android Facebook sdk从Facebook获取朋友列表?

更新时间:2023-10-11 21:43:04

获取登录到您的应用程序的用户的Facebook好友列表?听起来你需要执行一个Facebook图表请求来获取该列表,然后提取你想要的朋友。

https://developers.facebook.com/docs/graph-api/reference/user/friendlists/

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

由于member是friendlist中的一个边缘,您可以使用您的friendlist 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请求的文档中,您可以看到可以使用用户对象。我没有足够的代表发布另一个链接不幸的是。

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.