我正在尝试在我的应用程序中使用Facebook身份验证,但由于缺少令牌而无法登录。在LogCat中,我有一个循环:“ [Tag] System.out [Text] doLogin:”。
这发生在Facebook对话框之前,当我单击“确定”时,什么也没发生。
这是我的代码:
private SharedPreferences.Editor loginPrefsEditor;
private Session.StatusCallback statusCallback = new SessionStatusCallback();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fb_auth);
choosePseudoButton = (Button) findViewById(R.id.buttonChoosePseudoFb);
progressBarFb = (ProgressBar) findViewById(R.id.progressBarFbLoad);
textViewFbInfo = (TextView) findViewById(R.id.currentInfoFb);
editTextFbPseudo = (EditText) findViewById(R.id.editTextPseudoFb);
/*
* Get existing access_token if any
*/
mPrefs = getPreferences(MODE_PRIVATE);
// start Facebook Login
Session.openActiveSession(this, true, new Session.StatusCallback() {
public void call(final Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
System.out.println("onCreate");
fbToken = session.getAccessToken();
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token", fbToken);
JSONRequest jr = (JSONRequest) new JSONRequest().execute();
}
}
});
}
else {
doLogin();
}
}
});
}
private void doLogin() {
Session session = Session.getActiveSession();
if (!session.isOpened() && !session.isClosed()) {
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
} else {
Session.openActiveSession(this, true, statusCallback);
}
fbToken = session.getAccessToken();
System.out.println("doLogin: "+fbToken);
JSONRequest jr = (JSONRequest) new JSONRequest().execute();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
private void updateView() {
Session session = Session.getActiveSession();
if (session.isOpened()) {
fbToken = session.getAccessToken();
System.out.println("updateView: "+fbToken);
JSONRequest jr = (JSONRequest) new JSONRequest().execute();
}
else {
doLogin();
}
}
private class SessionStatusCallback implements Session.StatusCallback {
public void call(Session session, SessionState state, Exception exception) {
// Respond to session state changes, ex: updating the view
if( session.isOpened() ){
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
public void onCompleted(GraphUser user, Response response) {
}
});
}
updateView();
}
}
最佳答案
需要注意的几件事:
会话打开是异步发生的,这意味着当您调用Session.openActiveSession时,它不会(总是)立即打开会话(它必须调用另一个活动,等待用户输入等)。所以你的线
fbToken = session.getAccessToken();
几乎总是返回null。
其次,Session在进入OPENED状态之前可能处于几种状态,因此在SessionStatusCallback中,您可能应该将updateView()调用移至if(session.isOpened())检查中。
最后,您正在onCreate方法中调用Session.openActiveSession,在该回调中,如果未打开会话,则始终会调用doLogin()。但是就像我之前说的,会话可以在打开之前转换到多个状态,因此实际上您要多次调用doLogin。删除该doLogin()调用。