Getting all Facebook friends using Graph API

前端 未结 3 641
萌比男神i
萌比男神i 2021-01-15 05:38

I tried to GET \"me/friends\" and the returned JSON contains 3 less friends than my Facebook page shows that I have. It turned out that for those 3, GET \"/[id]\" returns fa

相关标签:
3条回答
  • 2021-01-15 06:16

    I don't think this is the cause. I was also struggling with this case. Using taggable_friends instead of friends will bring your friends.

    The point is that "friends" for facebook are those who use the same APP you are using/doing.

    Hope it helps.

    0 讨论(0)
  • 2021-01-15 06:21
    public class FbLogin extends AppCompatActivity {
        String fb_data_url = "url";
        CallbackManager callbackManager;
        LoginManager loginManager;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            FacebookSdk.sdkInitialize(getApplicationContext());
            setContentView(R.layout.activity_fb_login);
    
            fb_login_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    errorMsg.setText("");
                    facebookLogin();
                }
            });
    
    
            // Facebook login integration get all detail
            loginManager = LoginManager.getInstance();
            callbackManager = CallbackManager.Factory.create();
            LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    fbDialog.isShowing();
                    final AccessToken accessToken = loginResult.getAccessToken();
                    editor = sharedPreferences.edit();
                    editor.putString("FaceBookAccessToken", accessToken.getToken());
                    editor.apply();
    
                    GraphRequest request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            JSONObject fbJsonObjResponse = response.getJSONObject();
                            Log.e("FbResponse", response.toString());
                            try {
                                if (fbJsonObjResponse.has("id") && fbJsonObjResponse.getInt("id") > 0 && fbJsonObjResponse.has("birthday")) {
    
                                    fb_id_str = fbJsonObjResponse.optString("id");
                                    fb_name_str = fbJsonObjResponse.optString("name");
                                    fb_gender_str = fbJsonObjResponse.optString("gender");
                                    locale_str = fbJsonObjResponse.optString("locale");
                                    time_zone_str = fbJsonObjResponse.optString("timezone");
    
                                    fb_birthday_str = fbJsonObjResponse.optString("birthday");
    
                                    if (fb_birthday_str != null && !fb_birthday_str.equals("") && !fb_birthday_str.equals("null")) {
    
                                    } else {
                                        fb_birthday_str = "2018-02-14T08:13:18.951786+00:00";
                                    }
    
                                    String[] dob_format_arr = fb_birthday_str.split("/");
                                    String dob_format_month = dob_format_arr[0];
                                    String dob_format_day = dob_format_arr[1];
                                    String dob_format_year = dob_format_arr[2];
    
                                    String age = getAge(Integer.parseInt(dob_format_year), Integer.parseInt(dob_format_month), Integer.parseInt(dob_format_day));
                                    editor.putString("FbUserId", fb_id_str);
                                    editor.putString("UserName", fb_name_str);
                                    editor.putString("UserAge", age);
                                    editor.apply();
    
                                    fb_about_str = fbJsonObjResponse.optString("about");
                                    if (fb_about_str != null && !fb_about_str.equals("") && !fb_about_str.equals("null")) {
    
                                    } else {
                                        fb_about_str = getApplicationContext().getString(R.string.not_available);
                                    }
    
                                    fb_email_id_str = fbJsonObjResponse.optString("email");
                                    if (fb_email_id_str != null && !fb_email_id_str.equals("") && !fb_email_id_str.equals("null")) {
                                    } else {
                                        fb_email_id_str = getApplicationContext().getString(R.string.not_available);
                                    }
    
                                    fb_educationObj = fbJsonObjResponse.optString("education");
                                    if (fb_educationObj != null && !fb_educationObj.equals("") && !fb_educationObj.equals("null")) {
                                        JSONArray json_school = fbJsonObjResponse.optJSONArray("education");
                                        JSONObject json_school_obj = json_school.getJSONObject(0);
                                        JSONObject json_sc_obj = json_school_obj.optJSONObject("school");
                                        for (int j = 0; j < json_sc_obj.length(); j++) {
                                            education_str = json_sc_obj.optString("name");
                                        }
                                    } else {
                                        education_str = getApplicationContext().getString(R.string.not_available);
                                    }
    
                                    JSONObject fbLocation = new JSONObject();
                                    fb_location_str = fbJsonObjResponse.optString("location");
                                    if (fb_location_str != null && !fb_location_str.equals("null") && !fb_location_str.equals("")) {
                                        JSONObject json_location_obj = fbJsonObjResponse.optJSONObject("location");
                                        JSONObject jsonLocationObj = json_location_obj.optJSONObject("location");
                                        city_str = jsonLocationObj.optString("city");
                                        country_str = jsonLocationObj.optString("country");
                                        String locationID = json_location_obj.optString("id");
                                        fbLocation.put("id", locationID);
                                        fbLocation.put("name", city_str);
                                    } else {
                                        fbLocation = new JSONObject();
                                        city_str = getApplicationContext().getString(R.string.not_available);
                                        country_str = getApplicationContext().getString(R.string.not_available);
                                    }
    
                                    String firebase_token = sharedPreferences.getString("regId", null);
    
                                    favorite_teams_str = fbJsonObjResponse.optString("favorite_teams");
                                    JSONArray favorite_teamArray;
                                    if (favorite_teams_str != null && !favorite_teams_str.equals("") && !favorite_teams_str.equals("null")) {
                                        favorite_teamArray = fbJsonObjResponse.optJSONArray("favorite_teams");
                                    } else {
                                        favorite_teamArray = new JSONArray();
                                    }
                                    favorite_athletes_str = fbJsonObjResponse.optString("favorite_athletes");
                                    JSONArray favorite_athletesArray;
                                    if (favorite_athletes_str != null && !favorite_athletes_str.equals("") && !favorite_athletes_str.equals("null")) {
                                        favorite_athletesArray = fbJsonObjResponse.optJSONArray("favorite_athletes");
                                    } else {
                                        favorite_athletesArray = new JSONArray();
                                    }
    
                                    fb_education_str = fbJsonObjResponse.optString("education");
                                    JSONArray educationArray;
                                    if (fb_education_str != null && !fb_education_str.equals("") && !fb_education_str.equals("null")) {
                                        educationArray = fbJsonObjResponse.optJSONArray("education");
                                    } else {
                                        educationArray = new JSONArray();
                                    }
    
                                    fb_work_str = fbJsonObjResponse.optString("work");
                                    JSONArray json_workArray;
                                    if (fb_work_str != null && !fb_work_str.equals("") && !fb_work_str.equals("null")) {
                                        json_workArray = object.optJSONArray("work");
                                        JSONObject json_work_object = json_workArray.getJSONObject(0);
                                        JSONObject json_work_obj = json_work_object.optJSONObject("employer");
                                        {
                                            company_name_str = json_work_obj.optString("name");
                                        }
                                        String str_position_name = json_work_object.optString("position");
    
                                        if (str_position_name != null && !str_position_name.equals("") && !str_position_name.equals("null")) {
                                            JSONObject json_worker_pos = json_work_object.optJSONObject("position");
                                            position_name_str = json_worker_pos.optString("name");
                                        } else {
                                            //position_name_str = getApplicationContext().getString(R.string.not_available);
                                            position_name_str = "";
                                        }
    
                                    } else {
                                        json_workArray = new JSONArray();
                                        company_name_str = getApplicationContext().getString(R.string.not_available);
                                    }
    
                                    movies_str = fbJsonObjResponse.optString("movies");
                                    JSONArray moviesArray;
                                    if (movies_str != null && !movies_str.equals("null") && !movies_str.equals("")) {
                                        JSONObject moviesObject = new JSONObject(movies_str);
                                        moviesArray = moviesObject.optJSONArray("data");
                                    } else {
                                        moviesArray = new JSONArray();
                                    }
                                    musics_str = fbJsonObjResponse.optString("music");
                                    JSONArray musicArray;
                                    if (musics_str != null && !musics_str.equals("") && !musics_str.equals("null")) {
                                        JSONObject musicObject = new JSONObject(musics_str);
                                        musicArray = musicObject.optJSONArray("data");
                                    } else {
                                        musicArray = new JSONArray();
                                    }
                                    books_str = fbJsonObjResponse.optString("books");
                                    JSONArray bookSArray;
                                    if (books_str != null && !books_str.equals("") && !books_str.equals("null")) {
                                        JSONObject booksObject = new JSONObject(books_str);
                                        bookSArray = booksObject.optJSONArray("data");
                                    } else {
                                        bookSArray = new JSONArray();
                                    }
                                    tv_shows_str = fbJsonObjResponse.optString("television");
                                    JSONArray televisionArray;
                                    if (tv_shows_str != null && !tv_shows_str.equals("") && !tv_shows_str.equals("null")) {
                                        JSONObject televisionObject = new JSONObject(tv_shows_str);
                                        televisionArray = televisionObject.optJSONArray("data");
                                    } else {
                                        televisionArray = new JSONArray();
                                    }
    
                                    home_town_str = fbJsonObjResponse.optString("hometown");
                                    JSONObject json_homeTown_obj;
                                    if (home_town_str != null && !home_town_str.equals("") && !home_town_str.equals("null")) {
                                        json_homeTown_obj = fbJsonObjResponse.optJSONObject("hometown");
                                    } else {
                                        json_homeTown_obj = new JSONObject();
                                        json_homeTown_obj.put("id", "");
                                        json_homeTown_obj.put("name", "");
                                    }
    
                                    relationship_status = fbJsonObjResponse.optString("relationship_status");
                                    if (relationship_status != null && !relationship_status.equals("") && !relationship_status.equals("null")) {
                                        relationship_status_str = fbJsonObjResponse.optString("relationship_status");
                                    } else {
                                        relationship_status_str = getApplicationContext().getString(R.string.not_available);
                                    }
    
                                    JSONArray likesArray;
                                    fb_Likes_str = fbJsonObjResponse.optString("likes");
                                    if (fb_Likes_str != null && !fb_Likes_str.equals("") && !fb_Likes_str.equals("null")) {
                                        JSONObject likesObject = new JSONObject(fb_Likes_str);
                                        likesArray = likesObject.optJSONArray("data");
                                    } else {
                                        likesArray = new JSONArray();
                                    }
    
                                    JSONArray friendArr = new JSONArray();
                                    friends_str = fbJsonObjResponse.optString("friends");
                                    if (friends_str != null && !friends_str.equals("") && !friends_str.equals("null")) {
                                        JSONObject jsonObject = new JSONObject(friends_str);
                                        JSONArray jsonArray = jsonObject.optJSONArray("data");
                                        for (int i = 0; i < jsonArray.length(); i++) {
                                            JSONObject jsonFriend = jsonArray.optJSONObject(i);
                                            JSONObject jsonObject1 = new JSONObject();
                                            friend_str_ids = jsonFriend.optString("id");
                                            jsonObject1.put("id", friend_str_ids);
                                            friendArr.put(jsonObject1);
                                        }
                                    } else {
                                        friendArr = new JSONArray();
                                    }
    
                                    user_albums = fbJsonObjResponse.optString("albums");
                                    if (user_albums != null && !user_albums.equals("") && !user_albums.equals("null")) {
                                        JSONObject jsonAlbumObj = new JSONObject(user_albums);
                                        JSONArray jsonAlbumArray = jsonAlbumObj.optJSONArray("data");
                                        for (int i = 0; i < jsonAlbumArray.length(); i++) {
                                            JSONObject json_album = jsonAlbumArray.optJSONObject(i);
    
                                            String album_type = json_album.optString("type");
                                            if (album_type.contains("profile")) {
                                                album_id_str = json_album.getString("id");
                                            } else {
                                                //album_id_str = "";
                                            }
                                        }
                                    } else {
                                        //album_id_str = "";
                                    }
    
                                } catch(JSONException e){
                                    e.printStackTrace();
                                    Log.e("Create User Exception", e.getMessage());
                                }
                                Log.e("createUser", createUser.toString());
                                // Volley response fetch
                                JsonObjectRequest jsonObjReq = new JsonObjectRequest(
                                        com.android.volley.Request.Method.POST, fb_data_url, createUser,
                                        new com.android.volley.Response.Listener<JSONObject>() {
                                            @Override
                                            public void onResponse(JSONObject response) {
                                                try {
                                                    Log.e("Create User ", response.toString());
                                                    String apiResponse = response.getString("ApiResponse");
                                                    String UserPref = response.getString("UserPref");
                                                    String gender;
                                                    if (UserPref.equals("m")) {
                                                        gender = getApplicationContext().getString(R.string.male);
                                                    } else if (UserPref.equals("f")) {
                                                        gender = getApplicationContext().getString(R.string.female);
                                                    } else {
                                                        gender = getApplicationContext().getString(R.string.male_female_both);
                                                    }
                                                    editor.putString("InterestedInSp", gender);
                                                    editor.apply();
                                                    if ((apiResponse.contains("Error"))) {
                                                        errorMsg.setText(apiResponse);
                                                    } else {
                                                        editor.putString("fbLogging", "fbLogging");
                                                        JSONObject UserAuth_obj = response.getJSONObject("UserAuth");
                                                        MyUserID_str = UserAuth_obj.getString("MyUserID");
                                                        MyUserToken_str = UserAuth_obj.getString("MyUserToken");
                                                        editor.putString("MyUserIDSp", MyUserID_str);
                                                        editor.putString("MyUserTokenSp", MyUserToken_str);
                                                        editor.apply();
                                                        fbDialog.dismiss();
                                                        startActivity(new Intent(FbLogin.this, Home.class));
                                                    }
                                                } catch (JSONException e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        }, new com.android.volley.Response.ErrorListener() {
    
                                    @Override
                                    public void onErrorResponse(VolleyError error) {
                                        fbDialog.dismiss();
                                        errorMsg.setText("You Don~t have Permission Compulsory");
                                        Log.e("Error_Response", error.toString());
                                    }
                                }) {
                                    @Override
                                    public Map<String, String> getHeaders() throws AuthFailureError {
                                        HashMap<String, String> headers = new HashMap<String, String>();
                                        headers.put("Content-Type", "application/json; charset=utf-8");
                                        return headers;
                                    }
                                };
                                Apps.getInstance().addToRequestQueue(jsonObjReq);
                            } else{
                                fbDialog.dismiss();
                                LoginManager.getInstance().logOut();
                                errorMsg.setText("Permission Compulsory");
                                Toast.makeText(FbLogin.this, "Technical issue by user data.", Toast.LENGTH_SHORT).show();
                            }
                        } catch(
                        JSONException e)
    
                        {
                            fbDialog.dismiss();
                            LoginManager.getInstance().logOut();
                            errorMsg.setText("Permission Compulsory");
                            e.printStackTrace();
                            Log.e("Exception", e.getMessage());
                        }
                    }
                });
                Bundle parameters = new Bundle();
                    parameters.putString("fields","id,email,name,gender,birthday,about,picture.type(large),location{location},"+
                            "favorite_athletes,hometown,favorite_teams,education,work,movies,music,books,television,likes.limit(100){id,name,category,created_time},"+
                            "relationship_status,locale,timezone,friends,albums.limit(100){id,type}");
                    request.setParameters(parameters);
                    request.executeAsync();
            }
    
            @Override
            public void onCancel () {
                fbDialog.dismiss();
                LoginManager.getInstance().logOut();
                errorMsg.setText("Permission Compulsory");
                Toast.makeText(FbLogin.this, "Login Cancelled...", Toast.LENGTH_SHORT).show();
            }
    
            @Override
            public void onError (FacebookException exception){
                fbDialog.dismiss();
                LoginManager.getInstance().logOut();
                errorMsg.setText("Permission Compulsory");
                Toast.makeText(FbLogin.this, "Check your internet connection!", Toast.LENGTH_SHORT).show();
            }
        });
    }
    }
    
    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
            super.onActivityResult(requestCode,resultCode,data);
            callbackManager.onActivityResult(requestCode,resultCode,data);
            }
    
    private void facebookLogin(){
            fbDialog=new ProgressDialog(FbLogin.this);
            fbDialog.setProgressStyle(0);
            fbDialog.setMessage(getApplicationContext().getString(R.string.fetch_fb_data));
            fbDialog.show();
            LoginManager.getInstance().logInWithReadPermissions(this,
            Arrays.asList("public_profile,email,user_birthday,user_location,user_hometown,user_about_me,user_likes,user_education_history,user_work_history,user_relationships,user_friends,user_photos"));
            }
    
            }
    
    0 讨论(0)
  • 2021-01-15 06:26

    Users can opt out of the Facebook platform entirely by going to Privacy Settings -> Apps and Websites:

    enter image description here

    0 讨论(0)
提交回复
热议问题