我使用的是Facebook SDK 3.14,无法获取用户名和生日。
用户名应该是公共的,但它返回null。
对于出生日期,我添加了额外的权限,但仍然会返回null。
我的代码是:
public class FacebookLoginActivity extends ActionBarActivity
implements Session.StatusCallback {
/* ******************************
******* CONSTANTS **************
* **************************** */
private static final String TAG = "TAG_FacebookLoginActivity";
/**
* additional permissions.
* the basic permissions are granted by default
*/
private final List<String> PERMISSIONS =
Arrays.asList("email","user_birthday","public_profile");
/* ******************************
******* FIELDS *****************
* **************************** */
private UiLifecycleHelper mUiLifecycleHelper;
/* ******************************
******* METHODS ****************
* **************************** */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_facebook_login);
mUiLifecycleHelper = new UiLifecycleHelper(this, this);
mUiLifecycleHelper.onCreate(savedInstanceState);
initViews();
}
@Override
protected void onResume() {
super.onResume();
mUiLifecycleHelper.onResume();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mUiLifecycleHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onPause() {
super.onPause();
mUiLifecycleHelper.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
mUiLifecycleHelper.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mUiLifecycleHelper.onSaveInstanceState(outState);
}
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
Log.i(TAG, "Logged in...");
getUserInformation(session);
} else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
}
}
private void getUserInformation(Session session) {
Request.newMeRequest(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
Log.d(TAG, "Birthday: "+user.getBirthday());
Log.d(TAG, "UserName: "+user.getUsername());
}
}).executeAsync();
}
/**
* Initialize all views
*/
private void initViews() {
LoginButton loginButton = (LoginButton)findViewById(R.id.authButton);
// request more permissions
loginButton.setReadPermissions(PERMISSIONS);
}
}
谢谢!