1

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.

hg8
  • 1,082
  • 2
  • 15
  • 28
snehal_penurkar
  • 273
  • 1
  • 3
  • 15
  • Please post what you've tried so far. Also, not every user will have an email, they can also deny giving you email permissions. – Ming Li Jul 28 '15 at 17:40

2 Answers2

7

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();
snehal_penurkar
  • 273
  • 1
  • 3
  • 15
  • 3
    Actually, this doesn't always work, as I found out. For some reason, one of my FB accounts crashes with "No value for email", while the other ones return the email address as expected. The failing one is a newly-created account. I still don't know why this is happening. – FractalBob May 22 '17 at 19:11
  • I am facing the same problem with one of the accounts. Did you find the problem? – Gaurav Jul 09 '17 at 10:25
  • 1
    Check if that account has email id and has permission to share its details. Or let me know you can find any error logs there. – snehal_penurkar Jul 10 '17 at 02:10
  • 3
    Yes, some times API do not respond with email because user is operating his Facebook account with phone number and he/she don't have any email associated with Facebook account. – Muhammad Maqsood Sep 22 '17 at 12:55
  • This happens when the user has not attached a email to that account. he might be using a phone number to login ,which means he does not require a email to log – RAINA Dec 23 '19 at 06:18
  • This happens because the user uses mobile phone to login and has not yet attached any email address – Daniel Nyamasyo Jan 11 '20 at 10:15
0
    val email = if (userDataObject?.has("email") == true)userDataObject.getString("email") else "this account doesn't have email associated to FB account"