12

I integrated Facebook login in my android application. I want to get email id of login user. How will I get it?

private void loginToFacebook() {
    Session.openActiveSession(this, true, new Session.StatusCallback() {

        @Override
        public void call(Session session, SessionState state, Exception exception) {
            if (session.isOpened()) {
                Log.i(TAG, "Access Token" + session.getAccessToken());
                Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        if (user != null) {
                            try {

                                 userID =user.getId();
                                 provider="facebook";
                                 userName=user.getUsername();
                                 firstName=user.getFirstName();
                                 lastName=user.getLastName();
                                 Log.d("****User****", "details:" + user);
                                 }catch(Exception e){

Here is my code. i use Request.GraphUserCallback() method but there is no response of email from this method.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
user2541633
  • 123
  • 1
  • 1
  • 6

3 Answers3

34

Before calling Session.openActiveSession do this to get permissions add this:

List<String> permissions = new ArrayList<String>();
permissions.add("email");

The last parameter in Session.openActiveSession() should be permissions. Now you can access user.getProperty("email").toString().

EDIT:

This is the way I am doing facebook authorization:

List<String> permissions = new ArrayList<String>();
permissions.add("email");

loginProgress.setVisibility(View.VISIBLE);

//start Facebook session
openActiveSession(this, true, new Session.StatusCallback() {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        if (session.isOpened()) {
            //make request to the /me API
            Log.e("sessionopened", "true");
            Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
                @Override
                public void onCompleted(GraphUser user, Response response) {
                    if (user != null) {
                        String firstName = user.getFirstName();
                        String lastName = user.getLastName();
                        String id = user.getId();
                        String email = user.getProperty("email").toString();

                        Log.e("facebookid", id);
                        Log.e("firstName", firstName);
                        Log.e("lastName", lastName);
                        Log.e("email", email);
                    }
                }
            });
         }
     }
 }, permissions);

Add this method to your activity:

private static Session openActiveSession(Activity activity, boolean allowLoginUI, Session.StatusCallback callback, List<String> permissions) {
    Session.OpenRequest openRequest = new Session.OpenRequest(activity).setPermissions(permissions).setCallback(callback);
    Session session = new Session.Builder(activity).build();
    if (SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI) {
        Session.setActiveSession(session);
        session.openForRead(openRequest);
        return session;
    }
    return null;
}
Egor Neliuba
  • 14,784
  • 7
  • 59
  • 77
  • Show us your code and the exact line which causes NullPointerException so we can you. – Egor Neliuba Jul 12 '13 at 09:36
  • The method openActiveSession(Activity, boolean, Session.StatusCallback) in the type Session is not applicable for the arguments (Login, boolean, new Session.StatusCallback(){}, List). this is the error show in eclipse – user2541633 Jul 12 '13 at 09:40
  • it still give me same error. remove argument Description Resource Path Location Type The method openActiveSession(Activity, boolean, Session.StatusCallback) in the type Session is not applicable for the arguments (Login, boolean, new Session.StatusCallback(){}, List) – user2541633 Jul 12 '13 at 10:29
  • Oops, my bad, take a look at my answer again. – Egor Neliuba Jul 12 '13 at 10:37
  • i used this code it works in my application. but when i copy paste this code in asynctask method it gives me error. 07-17 10:37:13.971: E/AndroidRuntime(5608): java.lang.RuntimeException: An error occured while executing doInBackground(). – user2541633 Jul 17 '13 at 10:52
  • is it possible to do login with facebook and send data to webservice using async task method. cause logcat shows me network work on main thread warning. Please i need solution for this. – user2541633 Jul 17 '13 at 12:45
  • 2
    note, the executeMeRequestAsync() is now deprecated, but the Session.openActiveSession() method allows to include permissions as a parameter. – ılǝ May 12 '14 at 08:50
  • you saved my life! but permission is the third parameter, not the last :-D – D Ferra Jan 27 '15 at 11:08
  • Hi Egor, I am getting NullpointerException in this line " Session session = new Session.Builder(activity).build();". the error is 'The applicationId cannot be null'.. Please help me to solve this – Narendra Kumar Jul 30 '15 at 08:11
3

Following the suggestion of Egor N, I change my old code

Session.openActiveSession(this, true, statusCallback);

whith this new one:

List<String> permissions = new ArrayList<String>();
permissions.add("email");
Session.openActiveSession(this, true, permissions, statusCallback);

Now FB ask the user about the permission of email and I can read it in the response.

Alecs
  • 2,900
  • 1
  • 22
  • 25
-1

Try this article. Hope your issues will be solved Click Here

Btw, You need to use user.asMap().get("email").toString()); for receving the User Email ID. Also, you need to assign that into some of label like lblEmail.setText where lblEmail is a Textview.

Yuva Raj
  • 3,881
  • 1
  • 19
  • 30
  • i used simple android button. so how i can put permission there. now i just need to put permission there for email. But how i can put permission and where? – user2541633 Jul 12 '13 at 09:07
  • 1
    @user2541633 `LoginButton authButton = (LoginButton) findViewById(R.id.authButton); authButton.setOnErrorListener(new OnErrorListener() { authButton.setReadPermissions(Arrays.asList("basic_info","email"));` – Yuva Raj Jul 12 '13 at 11:30
  • This thing working on emulator but when i installed my app on phone it's directed to facebook android app but didn't give me any response – user2541633 Sep 19 '13 at 11:56