简体   繁体   中英

facebook friend list graph retrive empty array

I have logged in to facebook via the login button, i can get info on myself but from some reason when i try to get the friend list i get an empty json array in return. here is the sample code: (i call the graph request on the on success method of my facebook callback private class)

private CallbackManager callbackManager;

private FacebookCallback <LoginResult> mCallBack = new FacebookCallback <LoginResult> () {
  @Override
  public void onSuccess(LoginResult loginResult) {

    //some code

    GraphRequest request = GraphRequest.newMyFriendsRequest(
      AccessToken.getCurrentAccessToken(),
      new GraphRequest.GraphJSONArrayCallback() {
        @Override
        public void onCompleted(JSONArray array, GraphResponse response) {
          if (response != null) {
            Log.d("FB", "response isnt null");
          }

          if (array != null) {
            Log.d("FB", "array: " + array.toString());
          }
        }
      });

    Bundle parameters = new Bundle();
    parameters.putString("fields", "first_name,last_name,name,id");
    request.setParameters(parameters);
    request.executeAsync();
    Log.d("FB", "parameters: " + parameters.toString());


  }

  @Override
  public void onCancel() {
    statusText.setText("you have to connect");
  }

  @Override
  public void onError(FacebookException e) {

  }
};

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
  callbackManager = CallbackManager.Factory.create();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
  Bundle savedInstanceState) {
  return inflater.inflate(R.layout.fragment_main, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
  super.onViewCreated(view, savedInstanceState);

  statusText = (TextView) view.findViewById(R.id.status_text);
  loginButton = (LoginButton) view.findViewById(R.id.login_button);
  loginButton.setReadPermissions("user_friends");
  loginButton.setFragment(this);
  loginButton.registerCallback(callbackManager, mCallBack);
}

i tried both of this since the facebook graph query shows the id regardless me not asking him:

parameters.putString("fields", "first_name,last_name,name,id");
parameters.putString("fields", "first_name,last_name,name");

Did I miss something?


Answer: I have come to know that the graph api 2.0 and above doesn't show all your firends list, instead it will just show you the friends that installed your app and have connected to their FB account from your app, that is if you released your app to the play store. If your app is still under development and hasn't released yet you won't be able to see any friends unless you added them as "testers" and you/they installed the app on a phone/emulator and logged in from their account.

to add someone as a tester just go to facebook developers, and then: click on "my apps" at the top toolbar -> choose your app from the list -> click on "roles" on the left panel -> click on the "add testers" button -> pick your friends you want to add as testers.

FB docs on user_friends permissions says:

In order for a person to show up in one person's friend list, both people must have decided to share their list of friends with your app and not disabled that permission during login. Also both friends must have been asked for user_friends during the login process.

Are you sure that some of your friends have logged into your app too?


To see if the request is done correctly regardless of your code, you can use Graph API explorer . Make sure to select your app in top right corner select box, paste access token you get from your app (most likely AccessToken.getCurrentAccessToken() ) and try to make request to /me/friends . If the request in Graph API explorer returns some data, there is probably something wrong with your code. Otherwise it's some Facebook mechanism, like the one quoted above.

I have come to know that the graph api 2.0 and above doesn't show all your firends list, instead it will just show you the friends that installed your app and have connected to their FB account from your app, that is if you released your app to the play store. If your app is still under development and hasn't released yet you won't be able to see any friends unless you added them as "testers" and you/they installed the app on a phone/emulator and logged in from their account.

to add someone as a tester just go to facebook developers, and then: click on "my apps" at the top toolbar -> choose your app from the list -> click on "roles" on the left panel -> click on the "add testers" button -> pick your friends you want to add as testers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM