获取已安装该应用的Facebook好友列表

时间:2018-06-16 13:23:11

标签: java android facebook facebook-graph-api

我正在制作我的应用程序,我需要从Facebook用户那里获得一些信息,我有性别和区域设置,但我不知道如何让安装了应用程序的朋友。 到目前为止,这是我的代码:

public class FacebookUser {
private String gender, locale;
private FirebaseUser user;
private FirebaseAuth auth;
private Context context;



public FacebookUser(final Context context){
    gender=null;
    locale=null;
    this.context=context;
    auth=FirebaseAuth.getInstance();
    user=auth.getCurrentUser();

}
public void createUser(){
    FacebookSdk.sdkInitialize(context);
    GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
            new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject object, GraphResponse response) {
                    try{
                        Log.e("RESPONSE: ",response.getRawResponse());
                        gender = object.getString("gender");
                        locale = object.getString("locale");
                        User currentUser = new User(user.getDisplayName(), user.getEmail(),locale, gender, null,User.UNDEFINED, CuboRubik.UNDEFINED, 0, 0 ,0.00000001F,context);
                        User.putUserInShared(currentUser, context);
                        AddToDatabase addToDatabase = new AddToDatabase(context);
                        addToDatabase.createUser(currentUser, context);
                    }catch(JSONException e){
                        e.printStackTrace();
                    }
                }
            });
    Bundle bundle = new Bundle();
    bundle.putString("fields", "locale,gender");
    request.setParameters(bundle);
    request.executeAsync();
}

这是尝试获取朋友名单......

 private void friendGraphRequest(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");
                Log.e("USERNAME: ",""+usersName);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });
    Bundle param = new Bundle();
    param.putString("fields", "name");
    request.setParameters(param);
    request.executeAsync();
}

我知道somo有问题,但也许我可以使用一些代码。 谢谢!

1 个答案:

答案 0 :(得分:0)

friendGraphRequest代码中,您正在访问friendListId/members,这是访问'Friend lists'的旧API的一部分(Facebook用户将其朋友组织到不同的列表中) - 这可能不是你想要的

要访问用户的朋友列表,API调用为/<USER>/friendsor /me/friends为速记)

这将返回同时使用您的应用的用户的朋友的姓名和ID,以及所有用户的朋友的计数(在响应中的summary键中)

如果您想保留已有的代码,请将graphPath设为/me/friends并从那里开始工作