我们可以从Android Facebook Sdk获取用户的出生日期吗?

时间:2014-09-06 11:48:16

标签: android facebook

我需要从他们的FB帐户中获取用户的出生日期。我试过这个链接:Get Date of Birth and Town/City from Facebook SDK GraphUserCallback

但它对我帮助不大。请让我知道是否可以获取出生日期,或者FB SDk有任何限制来访问它?

感谢。

1 个答案:

答案 0 :(得分:1)

首先,您必须在使用Facebook SDK时指定 user_birthday 权限,然后您可以使用以下代码段来请求用户生日。

修改后的代码只是为了让您完整演示

    public void getBirthDate(){
    // set permissions 
    List<String> permissions = new ArrayList<String>();
    permissions.add("user_birthday");

    Session activeSession = Session.getActiveSession();
    Context context=getApplicationContext();
    if (activeSession == null || activeSession.getState().isClosed()) {
        Session session = new Session.Builder(context).build();
        Session.setActiveSession(session);
        // set active session
        activeSession = session;
    }
    // if a session is already open then issue a request using the available
    // session. Otherwise ask for user credentials.
    if (activeSession.isOpened()) {
        // User Logged In
        Session.getActiveSession().closeAndClearTokenInformation();

        // Open Active Session
        Session.openActiveSession(this, true, new Session.StatusCallback() {
            @Override
            public void call(final Session session, SessionState state, Exception exception) {
                if (session.isOpened()) {
                    Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
                        @Override
                        public void onCompleted(GraphUser user, Response response) {
                            if (user != null) {
                                Log.i(LOGTAG, "Json Data that is coming from FB:" + user.getInnerJSONObject());
                            }
                        }
                    });
                }
            }
        });
    } else {
        //If session is not opened 
        OpenRequest openRequest = new Session.OpenRequest((Activity) this);
        openRequest.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
        //Setting no call back dealing it in OnAcitivityResult
        openRequest.setCallback(null);

        //Setting Permission
        openRequest.setPermissions(permissions);
        activeSession.openForRead(openRequest);
    }
}

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

    Session activeSession = Session.getActiveSession();
    if (activeSession == null || activeSession.getState().isClosed()) {
        Session session = new Session.Builder(getApplicationContext()).build();
        Session.setActiveSession(session);
        activeSession = session;
    }

    if (activeSession.isOpened()) {
        Session.openActiveSession(this, true, new Session.StatusCallback() {
            @Override
            public void call(final Session session, SessionState state, Exception exception) {
                if (session.isOpened()) {
                    Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
                        @Override
                        public void onCompleted(GraphUser user, Response response) {
                            if (user != null) {
                                Log.i(LOGTAG, "Json Data that is coming from FB:" + user.getInnerJSONObject());
                            }
                        }
                    });
                }
            }
        });
    }
}

希望这会对你有所帮助

JSON回调看起来像

   {
   "id": "146473xxx",
   "name": "XYZ",
   "first_name": "dkajsd",
   "last_name": "kajs",
   "link": "https://....",
   "username": "xxxx",
   "birthday": "12/12/91",
   "hometown": 
   }