2013-03-19 47 views
4

如何獲得用戶生日user.getBirthday()如何從facebook sdk獲取用戶生日?

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

回答

7

首先你有權限設置user_birthday嗎?如果您要訪問user_birthday信息

如果用戶不爲空,通常你可以得到由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); 

該權限是含有您的許可

陣列
+0

嗨,我該如何設置權限?你能展示例子嗎? – user2185544 2013-03-19 09:31:18

+0

@ user2185544請參閱我編輯的答案:) – 2013-03-19 09:40:59

+0

什麼是authbutton? – 2014-12-01 15:46:45

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 - 你的活動環境。

希望這有助於某人.. :)

相關問題