0

I was give the permission to get the email from facebook account. Eventhough i couldn't get it. it always an empty string. here is my code. please help me.

i'm adding the code i use. it is copied from stackoverflow.

fbLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this, Arrays.asList("public_profile","email","basic_info")); } });

    callbackManager = CallbackManager.Factory.create();

    LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    GraphRequest.newMeRequest(
                            loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                                @Override
                                public void onCompleted(JSONObject user, GraphResponse response) {
                                    if (response.getError() != null) {
                                        // handle error
                                    } else {
                                        socialData = new ArrayList<NameValuePair>();
                                        String email = user.optString("email");
                                        String id = user.optString("id");
                                        String name = user.optString("name");
                                        socialData.add(new BasicNameValuePair("id",id));
                                        socialData.add(new BasicNameValuePair("name",name));
                                        socialData.add(new BasicNameValuePair("email",email));
                                        Log.e("id",id);
                                        Log.e("name",name);
                                        Log.e("email",email);
                                        Log.e("object",user.toString());
                                        // send email and id to your web server
                                        SocialLogin socialLogin = new SocialLogin();
                                        socialLogin.execute("app/social-login");

                                    }
                                }
                            }).executeAsync();

                }

                @Override
                public void onCancel() {
                    Toast.makeText(LoginActivity.this, "Login Cancel", Toast.LENGTH_LONG).show();
                }

                @Override
                public void onError(FacebookException exception) {
                    Toast.makeText(LoginActivity.this, exception.getMessage(), Toast.LENGTH_LONG).show();
                }
            });

'

and here is my output

E/id﹕ 1046433868724117
E/name﹕ Jafer Poovan P
E/email﹕ '

2 Answers2

2

Check out "declarative fields" in the changelog for v2.4: https://developers.facebook.com/docs/apps/changelog#v2_4

You now have to specify the fields in the API call, for example: /me?fields=name,email

...else you only get the fields "id" and "name".

andyrandy
  • 72,880
  • 8
  • 113
  • 130
0

try to use this code. It's work successfully.

loginButton = (LoginButton) findViewById(R.id.login_button);

List < String > permissionNeeds = Arrays.asList("user_photos", "email",
 "user_birthday", "public_profile", "AccessToken");
loginButton.registerCallback(callbackManager,
new FacebookCallback < LoginResult > () {@Override
 public void onSuccess(LoginResult loginResult) {

  System.out.println("onSuccess");

  String accessToken = loginResult.getAccessToken()
   .getToken();
  Log.i("accessToken", accessToken);

  GraphRequest request = GraphRequest.newMeRequest(
  loginResult.getAccessToken(),
  new GraphRequest.GraphJSONObjectCallback() {@Override
   public void onCompleted(JSONObject object,
   GraphResponse response) {
    Log.i("LoginActivity", response.toString());
    try {
     id = object.getString("id");
     try {
      URL profile_pic = new URL(
       "http://graph.facebook.com/" + id + "/picture?type=large");
      Log.i("profile_pic",
      profile_pic + "");

     } catch (MalformedURLException e) {
      e.printStackTrace();
     }
     name = object.getString("name");
     email = object.getString("email");
     gender = object.getString("gender");
     birthday = object.getString("birthday");
    } catch (JSONException e) {
     e.printStackTrace();
    }
   }
  });
  Bundle parameters = new Bundle();
  parameters.putString("fields",
   "id,name,email,gender, birthday");
  request.setParameters(parameters);
  request.executeAsync();
 }

 @Override
 public void onCancel() {
  System.out.println("onCancel");
 }

 @Override
 public void onError(FacebookException exception) {
  System.out.println("onError");
  Log.v("LoginActivity", exception.getCause().toString());
 }
});

For more info and fully facebook login code Check my answer HERE.

Community
  • 1
  • 1
Harvi Sirja
  • 2,472
  • 2
  • 18
  • 19