Search code examples
androidfacebookemailjsonexception

Android Facebook Graph API JSONException 'No value for email'


I am trying to integrate Facebook android sdk using this link. Everything runs perfect except I am getting one JSONException while trying to get email for logged user.

Below is the logcat I get,

07-28 16:22:37.420: W/System.err(15793): org.json.JSONException: No value for email 07-28 16:22:37.421: W/System.err(15793): at org.json.JSONObject.get(JSONObject.java:354) 07-28 16:22:37.421: W/System.err(15793): at org.json.JSONObject.getString(JSONObject.java:510) 07-28 16:22:37.421: W/System.err(15793): at in.airangle.foodapp.activities.TestActivity$1$1.onCompleted(TestActivity.java:83) 07-28 16:22:37.421: W/System.err(15793): at com.facebook.GraphRequest$1.onCompleted(GraphRequest.java:295) 07-28 16:22:37.421: W/System.err(15793): at com.facebook.GraphRequest$5.run(GraphRequest.java:1243) 07-28 16:22:37.421: W/System.err(15793): at android.os.Handler.handleCallback(Handler.java:615) 07-28 16:22:37.421: W/System.err(15793): at android.os.Handler.dispatchMessage(Handler.java:92) 07-28 16:22:37.422: W/System.err(15793): at android.os.Looper.loop(Looper.java:153) 07-28 16:22:37.422: W/System.err(15793): at android.app.ActivityThread.main(ActivityThread.java:5000) 07-28 16:22:37.422: W/System.err(15793): at java.lang.reflect.Method.invokeNative(Native Method) 07-28 16:22:37.422: W/System.err(15793): at java.lang.reflect.Method.invoke(Method.java:511) 07-28 16:22:37.422: W/System.err(15793): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821) 07-28 16:22:37.423: W/System.err(15793): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584) 07-28 16:22:37.423: W/System.err(15793): at dalvik.system.NativeStart.main(Native Method)

My understanding is, there should be way we can get user details using LoginManager and GraphRequest. If anyone has any idea please help me.


Solution

  • I got the answer. After successful facebook login, we need to create GraphRequest with accesstoken we get, in accordance to retrieveemail. Here is the code,

    GraphRequest request = GraphRequest.newMeRequest(accessToken,
    new GraphRequest.GraphJSONObjectCallback() {
    @Override
    public void onCompleted(JSONObject object,GraphResponse response) {
        if (response != null) {
            try {
                String mFbid = object.getString("id");
                String mFullname = object.getString("name");
                String email = object.getString("email");
            } catch (JSONException e) {}
        }
    }
    });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,email");
                request.setParameters(parameters);
                request.executeAndWait();