Facebook Android SDK:获得共同的朋友

时间:2013-03-16 04:33:04

标签: android facebook request

我正在尝试使用Android SDK获得两位Facebook用户的共同朋友:

Request friendsInCommon = Request.newRestRequest(myFbSession, "me/mutualfriends/otherUserId", null, HttpMethod.GET);

然而,这会返回以下错误:

03-16 04:24:39.652: D/ViewProfile(27121): My friends list: {Response:  responseCode: 200, graphObject: null, error: {HttpStatus: 200, errorCode: 3, errorType: null, errorMessage: Unknown method}, isFromCache:false}

关于我做错的任何线索?

由于

2 个答案:

答案 0 :(得分:2)

如果有人有兴趣,我最终通过将请求视为图谱API请求来解决这个问题:

Bundle params = new Bundle();
params.putString("fields", "id,name,picture");
Request req = new Request(myFbSession, "me/mutualfriends/otherUserId", params, HttpMethod.GET, new Callback(){
    @Override
    public void onCompleted(Response response) {
        // your callback code
    }
});
req.executeAsync();

答案 1 :(得分:1)

我知道回答这个问题为时已晚,但在Facebook Graph API v2.0及更高版本中已弃用 me / mutualfriends / otherUserId ,因此这是在Graph API中获取共同朋友的正确方法v2.0及以上

Bundle params = new Bundle();
            params.putString("fields", "context.fields(mutual_friends)");
            new GraphRequest(
                    AccessToken.getCurrentAccessToken(),
                    "/" + fbId,
                    params,
                    HttpMethod.GET,
                    new GraphRequest.Callback() {
                        @Override
                        public void onCompleted(GraphResponse graphResponse) {
                            try {
                                JSONObject jsonObject = new JSONObject(graphResponse.getRawResponse());
                                if (jsonObject.has("context")) {
                                   jsonObject = jsonObject.getJSONObject("context");
                                if (jsonObject.has("mutual_friends")) {
                                    JSONArray mutualFriendsJSONArray = jsonObject.getJSONObject("mutual_friends").getJSONArray("data");
                                  // this mutualFriendsJSONArray contains the id and name of the mutual friends.
                                }
                              }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
            ).executeAsync();

有关官方文档,请参阅此https://developers.facebook.com/docs/graph-api/reference/v2.3/user.context/mutual_friends