代码之家  ›  专栏  ›  技术社区  ›  Hector Chamorro Alvarez

获取安装了应用程序的Facebook好友列表

  •  0
  • Hector Chamorro Alvarez  · 技术社区  · 7 年前

    我正在制作我的应用程序,我需要从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();
    }
    

    我知道有些事情是错的,但也许我可以用一些代码。 谢谢!

    1 回复  |  直到 7 年前
        1
  •  0
  •   Igy    7 年前

    friendGraphRequest 代码,您正在访问 friendListId/members 它是旧api的一部分,用于访问 'Friend lists' (facebook用户用来将朋友组织成不同的列表)-这可能不是你想要的

    要访问用户的好友列表,api调用是 / <USER>/friends ( or /me/friends 作为速记)

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

    如果要保留已有的代码,请设置 graphPath /me/friends 在那里工作

    推荐文章