簡體   English   中英

獲取已安裝應用程序的Facebook朋友列表

[英]Get list of Facebook friends that have the app installed

我正在制作我的應用程序,我需要從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問題,但也許我可以使用其中一些代碼。 謝謝!

friendGraphRequest代碼中,您正在訪問friendListId/members ,這是舊API的一部分,用於訪問“朋友列表” (Facebook用戶用於將其朋友組織到不同列表中)-這可能不是您想要的

要訪問用戶的朋友列表,API調用為/ <USER>/friendsor /me/friends為簡寫)

這將返回還使用您的應用程序的用戶朋友的姓名和ID,以及所有用戶朋友的計數(在響應的summary鍵中)

如果要保留已有的代碼,請將graphPath設置為/me/friends並從那里開始

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM