Android how to get email from facebook sdk

喜你入骨 提交于 2019-12-10 12:15:17

问题


I need to get the email of the user using android Facebook sdk, I think it is something in the asMap but it seems it couldn't,

I tried this

    Session.openActiveSession(this, true, new Session.StatusCallback() {

        @Override
        public void call(Session session, SessionState state,
                Exception exception) {
            // TODO Auto-generated method stub
            if (session.isOpened()) {
                // make request to the /me API

                Request.executeMeRequestAsync(session,
                        new Request.GraphUserCallback() {

                            @Override
                            public void onCompleted(GraphUser user,
                                    Response response) {
                                if (user != null) {
                                    et_firstName.setText(user
                                            .getFirstName());
                                    et_lastName.setText(user
                                            .getLastName());
                                    Log.e("email",
                                            user.asMap().get("email")
                                                    + "");

                                } else {

                                }
                            }
                        });
            }
        }
    });

but the log gives me email = null. I am really confused why Facebook not giving us the email explicitly.

Any help would be appreciated, thanks in advance.

edit

Session sesssssss = null;
            sesssssss.openForRead(new Session.OpenRequest(this)
                    .setPermissions(null));
            Session.openActiveSession(this, true, new Session.StatusCallback() {

                @Override
                public void call(Session session, SessionState state,
                        Exception exception) {

                    // TODO Auto-generated method stub
                    if (session.isOpened()) {
                        // make request to the /me API

                        Request.executeMeRequestAsync(session,
                                new Request.GraphUserCallback() {

                                    @Override
                                    public void onCompleted(GraphUser user,
                                            Response response) {
                                        if (user != null) {
                                            et_firstName.setText(user
                                                    .getFirstName());
                                            et_lastName.setText(user
                                                    .getLastName());
                                            Log.e("data",
                                                    user.asMap().get("email")
                                                            + "");
                                            URL image_value;
                                            try {
                                                image_value = new URL(
                                                        "http://graph.facebook.com/"
                                                                + user.getId()
                                                                + "/picture");
                                                iv_profileImage
                                                        .setImageBitmap(BitmapFactory
                                                                .decodeStream(image_value
                                                                        .openConnection()
                                                                        .getInputStream()));
                                            } catch (MalformedURLException e) {
                                                e.printStackTrace();
                                            } catch (IOException e) {
                                                e.printStackTrace();
                                            }
                                        } else {

                                        }
                                    }
                                });
                    }
                }
            });

exception

07-31 14:55:44.054: E/AndroidRuntime(26398): FATAL EXCEPTION: main
07-31 14:55:44.054: E/AndroidRuntime(26398): java.lang.NullPointerException
07-31 14:55:44.054: E/AndroidRuntime(26398):    at com.Syriatel.EatTel.EditCustomerProfile.onOptionsItemSelected(EditCustomerProfile.java:89)
07-31 14:55:44.054: E/AndroidRuntime(26398):    at android.app.Activity.onMenuItemSelected(Activity.java:2205)
07-31 14:55:44.054: E/AndroidRuntime(26398):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:768)
07-31 14:55:44.054: E/AndroidRuntime(26398):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:147)
07-31 14:55:44.054: E/AndroidRuntime(26398):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
07-31 14:55:44.054: E/AndroidRuntime(26398):    at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
07-31 14:55:44.054: E/AndroidRuntime(26398):    at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
07-31 14:55:44.054: E/AndroidRuntime(26398):    at android.view.View$PerformClick.run(View.java:9229)
07-31 14:55:44.054: E/AndroidRuntime(26398):    at android.os.Handler.handleCallback(Handler.java:587)
07-31 14:55:44.054: E/AndroidRuntime(26398):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-31 14:55:44.054: E/AndroidRuntime(26398):    at android.os.Looper.loop(Looper.java:130)
07-31 14:55:44.054: E/AndroidRuntime(26398):    at android.app.ActivityThread.main(ActivityThread.java:3701)
07-31 14:55:44.054: E/AndroidRuntime(26398):    at java.lang.reflect.Method.invokeNative(Native Method)
07-31 14:55:44.054: E/AndroidRuntime(26398):    at java.lang.reflect.Method.invoke(Method.java:507)
07-31 14:55:44.054: E/AndroidRuntime(26398):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
07-31 14:55:44.054: E/AndroidRuntime(26398):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
07-31 14:55:44.054: E/AndroidRuntime(26398):    at dalvik.system.NativeStart.main(Native Method)

回答1:


Try something like this:

Session session = new Session(this); // <-- where "this" is a reference to your Activity, or Context
Session.OpenRequest request = new Session.OpenRequest(this).setPermissions("basic_info", "email");
openRequest.setCallback(new Session.StatusCallback() {
    ...
    // Put your callback code here
});
Session.setActiveSession(session);
session.openForRead(request);



回答2:


Try this

 Log.e("data", user.getProperty("email")+ "");

and add permissions which is null in your code.Add permissions like below

Arrays.asList("email", "user_birthday");

I am using the same code and its working .



来源:https://stackoverflow.com/questions/17942963/android-how-to-get-email-from-facebook-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!