Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get email id from facebook sdk in android applications?

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.

like image 494
user2541633 Avatar asked Jul 12 '13 07:07

user2541633


People also ask

How do I find an email address on Facebook Android?

Click the arrow in the upper-right corner of your news feed and select "Account Settings." Your email address is listed under General Account Settings.

What is a FB SDK?

The Facebook SDK is a set of software components that developers can include in their mobile app to understand how people use the app, run optimized marketing campaigns and enable Facebook login and social sharing. This course helps you understand the purpose of the Facebook SDK and App Events for Android and iOS.


1 Answers

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;
}
like image 66
Egor Neliuba Avatar answered Sep 28 '22 10:09

Egor Neliuba