user237462
user237462

Reputation: 421

Android - Facebook API returns no email

Heres the method im using and it returns the id and name no problems.

    // Private method to handle Facebook login and callback
    private void onFblogin() {
        callbackmanager = CallbackManager.Factory.create();

        // Set permissions
        LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email", "public_profile"));

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

                        System.out.println("Success");
                        GraphRequest.newMeRequest(
                                loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                                    @Override
                                    public void onCompleted(JSONObject json, GraphResponse response) {
                                        if (response.getError() != null) {
                                            // handle error
                                            System.out.println("ERROR");
                                        } else {
                                            System.out.println("Success");
                                            try {

                                                String jsonresult = String.valueOf(json);
                                                System.out.println("JSON Result" + jsonresult);

                                                // String str_email = json.getString("email");
                                                String str_email = json.getString("email");
                                                String str_name = json.getString("name");
                                                String str_id = json.getString("id");
//

                                                // Save your info
                                                settings = getSharedPreferences("login_details", 0);
                                                SharedPreferences.Editor editor = settings.edit();
                                                editor.putString("name", str_name);
                                                editor.putString("email", str_email);
                                                editor.commit();



                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    }

                                }).executeAsync();

                        Intent openNewActivity = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(openNewActivity);

                    }

                    @Override
                    public void onCancel() {
                        Log.d("TAG_CANCEL", "On cancel");
                    }

                    @Override
                    public void onError(FacebookException error) {
                        Log.d("TAG_ERROR", error.toString());
                    }
                });





    }

The problem i have is that email doesnt return.

Ive tested the permissions with another app that can get my email when i sign in so permissions seem fine. Im assuming im calling it wrong now.

Upvotes: 0

Views: 72

Answers (1)

Eenvincible
Eenvincible

Reputation: 5626

Try this just before executing your AsyncTask.

You can do this inside your onSuccess() method of the FacebookCallback().

 GraphRequest request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback(){
    @Override
    public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
             try {
                 email = jsonObject.getString("email");

                 fullName = jsonObject.getString("name");          


              } catch (Exception e) {
                    Log.d("FacebookActivity", "onCompleted - undetermined FB exception");
              }

           }
     });

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

@Override public void onCancel(){}

@Override public void onError(){}

Good luck!

Upvotes: 1

Related Questions