Android Facebook Login API returns only name and id

Total Post:24

Points:170

 557  View(s)
Ratings:
Rate this:

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.

  1. Re: Android Facebook Login API returns only name and id

    After long study, we find the solution of my problem.

    First of all, I allow the Facebook permission for our Android app and after that, we add the following code.


    loginButton = (LoginButton) findViewById(R.id.fb_login_button);
    FacebookSdk.sdkInitialize(getApplicationContext()); 
    AppEventsLogger.activateApp(this);
    callbackManager = CallbackManager.Factory.create();  
    loginButton.setReadPermissions(Arrays.asList("email","public_profile"));
    GraphRequest request = GraphRequest.newMeRequest( accessToken,
            new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted( JSONObject object, GraphResponse response) { 
                      …………………. String email=object.getString("email");
                   ………………. // Application code
                }
            });
    Bundle parameters = new Bundle();
    parameters.putString("fields", "id,name,email"); // You can add any field as per requirement
    request.setParameters(parameters);
    request.executeAsync(); 


Answer

Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.