forum

Home / DeveloperSection / Forums / Android Facebook Login API returns only name and id

Android Facebook Login API returns only name and id

Arti Mishra 1263 17-Sep-2018

Before Facebook Login in an android app, I’m Submit the request and  GraphResponce Object return only ........

GraphResponce : { Response: responseCode: 200, graphObject: {"name":"Development Mindstick","id":"219688002317931"}, error: null}
JSONObject: {"name":"Development Mindstick","id":"219688002317931"}

My Code :
loginButton = (LoginButton) findViewById(R.id.fb_login_button);
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
callbackManager = CallbackManager.Factory.create(); // create instance of callback manager class
if (Utils.isNetworkAvailable(getApplicationContext())) {
   loginButton.setReadPermissions(Arrays.asList(EMAIL, PUBLIC_PROFILE));
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
          accessTokenFacebook = loginResult.getAccessToken();
            final GraphRequest request = GraphRequest.newMeRequest(accessTokenFacebook, new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject object, GraphResponse response) {
                    Log.i("GraphResponse", response.toString());
                    Log.i("JSONObject", object.toString());
                    // Get facebook data from login
                    String email="";
                    try {
                       String userName = object.getString("name");
                       String id = object.getString("id");
                       email=object.getString("email");  // Error Generate
                        String firstName = " ", lastName = " ";
                        if (userName != null) {
                            int end = userName.lastIndexOf(' ');                             if (end >= 0) {                                 firstName = userName.substring(0, end);                                 lastName = userName.substring(end + 1, userName.length());                             }                         }                         Toast.makeText(getApplicationContext(),"Facebook Login Successful",Toast.LENGTH_SHORT).show();                         if(email.equals("")) {                          Toast.makeText(getApplicationContext(),getResources().getString(R.string.email_permission),Toast.LENGTH_SHORT).show();                         }                         else {                             loginApi("facebook", id, firstName, lastName, email);                         }                     } catch (Exception e) {                         e.printStackTrace();                     }                 }             });             request.executeAsync();         }         @Override         public void onCancel() {             Log.d("facebook Login","On cancel");         }         @Override         public void onError(FacebookException error) {             Log.v("Error", error.toString());         }     }); } else {     Toast.makeText(this, getResources().getString(R.string.no_internet), Toast.LENGTH_SHORT).show(); }

Gradle
implementation 'com.facebook.android:facebook-login:[4,5)'

But my need is GraphResponse object also return user email which is not easily possible in my code.


Updated on 02-Jul-2023

Can you answer this question?


Answer

2 Answers

Liked By