Android如何从facebook sdk获取电子邮件

时间:2013-07-30 09:17:18

标签: android facebook facebook-graph-api facebook-android-sdk facebook-sdk-3.0

我需要使用Android Facebook sdk获取用户的电子邮件,我认为这是asMap中的内容,但似乎不能,

我试过这个

    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 {

                                }
                            }
                        });
            }
        }
    });

但是log给了我email = null。我真的很困惑为什么Facebook没有明确地向我们发送电子邮件。

任何帮助将不胜感激,提前谢谢。

修改

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 {

                                        }
                                    }
                                });
                    }
                }
            });

例外

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)

2 个答案:

答案 0 :(得分:2)

尝试这样的事情:

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);

答案 1 :(得分:0)

试试这个

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

并添加代码中为null的权限。添加权限,如下所示

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

我使用相同的代码及其工作。