KLAng
KLAng

Reputation: 45

Android Facebook sdk 4.2 cannot get email

I am using facebook sdk 4.2 for my android app. But I was unable to get the my email and it show no value for email. However, I can get other value such as name, link, id, gender. There are some topic about this from stackoverflow but they are using the older sdk version, the class is different. Please help, here is my code.

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

LoginManager.getInstance().registerCallback(callbackManager,
  new FacebookCallback < LoginResult > () {
   @Override
    public void onSuccess(LoginResult loginResult) {

      Toast.makeText(MainActivity.this, "SUCCESS", Toast.LENGTH_SHORT).show();

      GraphRequest request = GraphRequest.newMeRequest(
        loginResult.getAccessToken(),
        new GraphRequest.GraphJSONObjectCallback() {
        @Override
          public void onCompleted(
            JSONObject object,
            GraphResponse response) {
            // Application code 
            try {
              String id = object.getString("id");
              String name = object.getString("name");

              String email = object.getString("email"); //Get null value here

              String gender = object.getString("gender");

              Toast.makeText(MainActivity.this, "HI," + name + "Gender: " + gender, Toast.LENGTH_SHORT).show();

            } catch (JSONException e) {
              Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
              e.printStackTrace(); 

            }
          }
        });
      Bundle parameters = new Bundle();
      parameters.putString("fields", "id,name,email,gender");
      request.setParameters(parameters);
      request.executeAsync();
    }

    @Override
    public void onCancel() {
      // App code
    }

    @Override
    public void onError(FacebookException exception) {
      // App code   
    }
  });
@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   super.onActivityResult(requestCode, resultCode, data);
   callbackManager.onActivityResult(requestCode, resultCode, data);
 }

My log file

06-05 13:47:56.207: W/System.err(9965): org.json.JSONException: No value for email 06-05 13:47:56.246: W/System.err(9965): at org.json.JSONObject.get(JSONObject.java:355) 06-05 13:47:56.246: W/System.err(9965): at org.json.JSONObject.getString(JSONObject.java:515) 06-05 13:47:56.254: W/System.err(9965): at com.android.jobstreet.MainActivity$2$1.onCompleted(MainActivity.java:242) 06-05 13:47:56.254: W/System.err(9965): at com.facebook.GraphRequest$1.onCompleted(GraphRequest.java:295) 06-05 13:47:56.254: W/System.err(9965): at com.facebook.GraphRequest$5.run(GraphRequest.java:1243) 06-05 13:47:56.254: W/System.err(9965): at android.os.Handler.handleCallback(Handler.java:733) 06-05 13:47:56.254: W/System.err(9965): at android.os.Handler.dispatchMessage(Handler.java:95) 06-05 13:47:56.254: W/System.err(9965): at android.os.Looper.loop(Looper.java:136) 06-05 13:47:56.254: W/System.err(9965): at android.app.ActivityThread.main(ActivityThread.java:5146) 06-05 13:47:56.254: W/System.err(9965): at java.lang.reflect.Method.invokeNative(Native Method) 06-05 13:47:56.254: W/System.err(9965): at java.lang.reflect.Method.invoke(Method.java:515) 06-05 13:47:56.254: W/System.err(9965): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732) 06-05 13:47:56.254: W/System.err(9965): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566) 06-05 13:47:56.254: W/System.err(9965): at dalvik.system.NativeStart.main(Native Method)

Upvotes: 4

Views: 3641

Answers (2)

ajey
ajey

Reputation: 466

You Must set following permission

loginbtn.setReadPermissions("email");

Upvotes: 6

QuotidianVoid
QuotidianVoid

Reputation: 619

You must request the "email" permission when you log the user in. Even if your app has the email permission, it will not always return an email address, so your app should not rely on that field always having a value.

More information from the Facebook documentation.

Upvotes: 0

Related Questions