Search code examples
androidfacebookfacebook-android-sdk

Get username in Facebook SDK 4 android


I need to get the username of a Profile after logging in. this is my code:

    @Override
    public void onSuccess(LoginResult loginResult) {
       
        GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject me, GraphResponse response) {
                        if (response.getError() != null) {
                            // handle error
                        } else {
                            String email = me.optString("email");
                            String id = me.optString("id");
                            String nick = me.optString("username ");
                            if(nick==""){
                                nick = me.optString("name");
                            }
                            // send email and id to your web server
                            ArrayList<String> keys = new ArrayList<String>(Arrays.asList("idGoogle","email","idEntity","nickname"));
                            ArrayList<String> values = new ArrayList<String>(Arrays.asList(id, email, entity.getIDENTITY().toString(),nick));
                            
                            new loginUser(keys,values,"newFacebookUser",id,FACEBOOK).execute();
                        }
                    }
                }).executeAsync();
   }    
   

What do I have to do to get the username of the profile?


Solution

  • /me/username is no longer available.

    Source

    Since v2.0 of the Graph API, it´s not possible to get the username anymore. Use the (App Scoped) ID to identify users.