如何从facebook sdk获得用户生日?

时间:2013-03-19 08:53:46

标签: android facebook sdk

如何通过user.getBirthday()获取用户生日?

Session.openActiveSession(this, true, new Session.StatusCallback() {
      // callback when session changes state
      @Override
      public void call(Session session, SessionState state, Exception exception) {
        if (session.isOpened()) {

          // make request to the /me API
          Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

            // callback after Graph API response with user object
            @Override
            public void onCompleted(GraphUser user, Response response) {
              if (user != null) {
                TextView welcome = (TextView) findViewById(R.id.textView1);
                welcome.setText("Hello " + user.getBirthday() + "!");
                Log.d("user_birthday","user_birthday:"+user.getBirthday());//user.getBirthday()  this value always is null?
              }
            }
          });
        }
      }
    });

2 个答案:

答案 0 :(得分:7)

首先,您是否已设置user_birthday的权限?如果您想访问user_birthday信息,这一点非常重要

如果用户通常不是null,则可以通过user.getBirthday()获取生日 因为从我看到你正在使用新的facebook SDK

您可以设置权限,例如使用facebook authbutton

authButton.setReadPermissions(Arrays.asList("user_location", "user_birthday", "user_likes"));

或者您可以重新授权

Session.ReauthorizeRequest reauthRequest = new Session.ReauthorizeRequest(this, PERMISSIONS).
                    setRequestCode(REAUTHORIZE_ACTIVITY).
                    setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
session.reauthorizeForPublish(reauthRequest);

PERMISSION是包含您的许可的数组

答案 1 :(得分:0)

小提示:可以使用初始开放会话请求请求权限,因此无需重新授权:

final Session.OpenRequest request = new Session.OpenRequest(Activity.this);
request.setPermissions(Arrays.asList("email", "user_birthday")); // Your permissions here
request.setCallback(new Session.StatusCallback()
{
    @Override
    public void call(final Session session, SessionState state, Exception exception)
    {
        if (session.isOpened())
        {
            // Your code here
        }
    }
});

Session fbSession = Session.getActiveSession();
if (fbSession == null || fbSession.isClosed())
{
    fbSession = new Session(Activity.this);
    Session.setActiveSession(fbSession);
}

fbSession.openForRead(request);

Activity.this - 您的活动背景。

希望这有助于某人...... :)