Facebook SDK电子邮件有时会返回null - Android

时间:2013-08-04 08:36:13

标签: android eclipse facebook sdk

当我试图从Android Facebook SDK获取用户的电子邮件时,我遇到了问题。我已经设置了正确的权限,它在某些情况下有效(我可以从用户那里获取所有数据),但有时在某些设备中,它不起作用。此外,我已禁用沙盒模式,我已在facebook的网站上注册了我的证书。拜托,任何人都可以帮助我吗?谢谢!!!!

private void signInWithFacebook() {

   SessionTracker mSessionTracker = new SessionTracker(getBaseContext(), new StatusCallback() {

        @Override
        public void call(Session session, SessionState state, Exception exception) {
        }
    }, null, false);

    String applicationId = Utility.getMetadataApplicationId(getBaseContext());
    mCurrentSession = mSessionTracker.getSession();

    if (mCurrentSession == null || mCurrentSession.getState().isClosed()) {
        mSessionTracker.setSession(null);
        Session session = new Session.Builder(getBaseContext()).setApplicationId(applicationId).build();
        Session.setActiveSession(session);
        mCurrentSession = session;
    }

    if (!mCurrentSession.isOpened()) {
        Session.OpenRequest openRequest = null;
        openRequest = new Session.OpenRequest(Main.this);


        if (openRequest != null) {
            openRequest.setDefaultAudience(SessionDefaultAudience.FRIENDS);
            openRequest.setPermissions(Arrays.asList("user_birthday", "email"));
            openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
            mCurrentSession.openForRead(openRequest);
        }
    }
    else{
        Request.executeMeRequestAsync(mCurrentSession, new Request.GraphUserCallback() {
            public void onCompleted(GraphUser user, Response response) {


                if(!mCurrentSession.getPermissions().contains("email")){

                    id = user.getId();
                    lastname = user.getName();
                    firstname = user.getFirstName();
                    email = user.getProperty("email").toString();
                    image = "http://graph.facebook.com/" + user.getId() + "/picture?type=large";
                    link = user.getLink();
                    ages = user.getBirthday();
                    sex = user.getProperty("gender").toString();

                    new CheckUser().execute();

                }
                else{

                    Toast.makeText(getApplicationContext(), "No email", Toast.LENGTH_LONG).show();




                }



            }
        });
    }


}

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode,
                resultCode, data);

        if (mCurrentSession.isOpened()) {
            Request.executeMeRequestAsync(mCurrentSession, new Request.GraphUserCallback() {
                public void onCompleted(GraphUser user, Response response) {

                    id = user.getId();
                    lastname = user.getName();
                    firstname = user.getFirstName();
                    email = user.getProperty("email").toString();
                    image = "http://graph.facebook.com/" + user.getId() + "/picture?type=large";
                    link = user.getLink();
                    ages = user.getBirthday();
                    sex = user.getProperty("gender").toString();


                    new CheckUser().execute();


                }
            });
        } else {
            Session.OpenRequest openRequest = null;
            openRequest = new Session.OpenRequest(Main.this);

            openRequest.setDefaultAudience(SessionDefaultAudience.FRIENDS);
            openRequest.setPermissions(Arrays.asList("user_birthday", "email"));
            openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);

            mCurrentSession.openForRead(openRequest);


        }

    }

0 个答案:

没有答案