Search code examples
facebook-graph-apifacebook-android-sdk

Accessing public information using a specific userID on facebook graph api


Someone is logged into my app. I need to display some profiles of people that might not be on this person's friends list. Is that possible, or is it a privacy issue. A person's "public profile" should be accessible right?

I tried to get the first name, profile picture and gender

Bundle parameters = new Bundle();
parameters.putString("fields", "first_name,gender");

try {
    new GraphRequest(
            AccessToken.getCurrentAccessToken(),
            "/" + user.getUserID(),
            parameters,
            HttpMethod.GET,
            new GraphRequest.Callback() {
                public void onCompleted(GraphResponse response) {
                    JSONObject facebookResponse = response.getJSONObject();
                    try {
                        final String first_name = facebookResponse.getString("first_name");
                        final String gender = facebookResponse.getString("gender");

                        Bundle params = new Bundle();
                        params.putBoolean("redirect", false);
                        params.putString("type", "square");
                        params.putInt("width", 200);
                        params.putInt("height", 200);

                        new GraphRequest(
                                AccessToken.getCurrentAccessToken(),
                                "/" + user.getUserID() + "/picture",
                                params,
                                HttpMethod.GET,
                                new GraphRequest.Callback() {
                                    public void onCompleted(GraphResponse response) {
                                        JSONObject facebookResponse = response.getJSONObject();
                                        try {
                                            JSONObject data = facebookResponse.getJSONObject("data");
                                            String url = data.getString("url");

                                            Name.setText(first_name);
                                            Gender.setText(gender);
                                            mNetworkImageView.setImageUrl(url, mImageLoader);

                                            facebookResponseListener.updatedUserObject(first_name, url, gender, pos);

                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }
                        ).executeAsync();

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
    ).executeAsync();

} catch (Exception uee) {
    uee.printStackTrace();
}

all of this is inside the getView() if a listview (it's where I display the profiles). While testing, I'm getting all the information of the friends of the logged in person, but the "gender" property in the response object is missing from the non-friend. Only the name and userID is being returned.


Solution

  • It's all explained in the docs (https://developers.facebook.com/docs/facebook-login/permissions):

    gender & locale can only be accessed if:

    The person queried is the person using the app.

    The person queried is using the app, and is a friend of the person using the app.

    The person queried is using the app, is not a friend of the person using the app, but the app includes either an app access token or an appsecret_proof argument with the call.