Akshay Shah
Akshay Shah

Reputation: 490

Facebook Android SDK 4.0 not returning emailID

I am trying to implement facebook login using latest facebook SDK 4.0.It is only returning User-ID and User-name. Below is the code which I have implemented using GRAPH APIs.

@Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
        textView = (TextView) view.findViewById(R.id.textView);

        List<String> permissions = new ArrayList<>();
        //permissions.add("user_friends");
        permissions.add("email");
        //permissions.add("user_birthday");
        loginButton.setReadPermissions(permissions);
        loginButton.setFragment(this);
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),new GraphRequest.GraphJSONObjectCallback() {

                            @Override
                            public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
                                if (graphResponse.getError() != null) {
                                    // handle error
                                } else {
                                    String email = jsonObject.optString("email");
                                    String id = jsonObject.optString("id");
                                    new GraphRequest(
                                            AccessToken.getCurrentAccessToken(),
                                            "/" + id,
                                            null,
                                            HttpMethod.GET,
                                            new GraphRequest.Callback() {
                                                @Override
                                                public void onCompleted( GraphResponse graphResponse) {
                                                        Log.d("FACEBOOK LOGIN","graph response" +graphResponse.toString());
                                                }
                                            }
                                    ).executeAsync();
                                    Log.d("FACEBOOK LOGIN ","email id and the user_id of the looged in user is = ::" + email+" ::"+id+"" +
                                            "JsonObject from facebook i am getting is :" +jsonObject.toString());
                                    // send email and id to your web server
                                }

                            }

                        }).executeAsync();
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException e) {

            }
        });

    }

Also I tried using GRAPH API EXPLORER, where it is showing me my email address as well as birthday and gender too. Below is the snapshot of it

Snapshot of GraphAPI explorer

Upvotes: 0

Views: 1141

Answers (2)

rushank shah
rushank shah

Reputation: 856

Try this .

    String username,email ;

    loginButton.registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        new fblogin().execute(loginResult.getAccessToken());

                    }

                    @Override
                    public void onCancel() {

                    }

                    @Override
                    public void onError(FacebookException e) {

                    }
                });




class fblogin extends AsyncTask<AccessToken, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Login.this);
            pDialog.setMessage("Loading Data..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        protected String doInBackground(AccessToken... params) {
            GraphRequest request = GraphRequest.newMeRequest(params[0],
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object,
                                GraphResponse response) {

                            try {

                                username = object.getString("first_name");

                                emailid = object.getString("email");


                            } catch (JSONException e) {
                                // TODO Auto-generated catch
                                // block
                                e.printStackTrace();
                            }
                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields",
                    "id,first_name,email,gender,birthday");
            request.setParameters(parameters);
            request.executeAndWait();

            return null;
        }

        protected void onPostExecute(String file_url) {

            pDialog.dismiss();

        }

    }

Its done.!!

Upvotes: 2

Anand Savjani
Anand Savjani

Reputation: 2535

try this one

loginButton.setReadPermissions(Arrays.asList("public_profile", "user_friends", "email"));

Upvotes: 0

Related Questions