我在UI中创建了一个按钮,在点击时应该调用一个函数(fbLogin()),它打开一个Session(如果已关闭)并授权用户。
public void fbLogin(View button) {
Log.e(getClass().getName(), "0");
Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
Session session = Session.getActiveSession();
if (session == null) {
Log.e(getClass().getName(), "1");
if (savedInstanceState != null) {
Log.e(getClass().getName(), "2");
session = Session.restoreSession(this, null, statusCallback,
savedInstanceState);
}
if (session == null) {
Log.e(getClass().getName(), "3");
session = new Session(this);
}
Log.e(getClass().getName(), "4");
Session.setActiveSession(session);
if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
Log.e(getClass().getName(), "5");
session.openForRead(new Session.OpenRequest(this)
.setCallback(statusCallback));
}
}
Log.e(getClass().getName(), "6");
updateView();
}
updateView():
private void updateView() {
final Session session = Session.getActiveSession();
if (session.isOpened()) {
Log.e(getClass().getName(), "7");
Request.executeMeRequestAsync(session,
new Request.GraphUserCallback() {
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user,
Response response) {
Log.e(getClass().getName(), "8");
if (user != null) {
String str = user.getName();
Intent intent = new Intent(
getApplicationContext(), LoggedIn.class);
Log.e(getClass().getName(), user.getName());
intent.putExtra("UserName", str);
startActivity(intent);
}
}
});
}
else {
Log.e(getClass().getName(), "9");
Session.openActiveSession(this, true, statusCallback);
if(session.isOpened()) Log.e(getClass().getName(), "15");
Log.e(getClass().getName(), "AT : "+ session.getAccessToken());
Request.executeMeRequestAsync(session,
new Request.GraphUserCallback() {
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user,
Response response) {
Log.e(getClass().getName(), "10");
if(user==null) Log.e(getClass().getName(), "11");
if (user != null) {
Log.e(getClass().getName(), "12");
String str = user.getName();
Intent intent = new Intent(
getApplicationContext(), LoggedIn.class);
Log.e(getClass().getName(), user.getName());
intent.putExtra("UserName", str);
startActivity(intent);
}
}
});
}
}
我的SessionStatusCallback类:
private class SessionStatusCallback implements Session.StatusCallback {
@Override
public void call(Session session, SessionState state,
Exception exception) {
Log.e(getClass().getName(), session.getState().toString());
Log.e(getClass().getName(), "13");
// updateView();
}
}
在SessionStatusCallback类中,它始终首先将会话状态显示为OPENING,然后显示CLOSED_LOGIN_FAILED。为什么状态正在变为CLOSED_LOGIN_FAILED。
Logcat输出:
08-12 17:56:31.928: E/com.example.usemeone.UsemeOne(966): 0
08-12 17:56:31.948: E/com.example.usemeone.UsemeOne(966): 1
08-12 17:56:31.948: E/com.example.usemeone.UsemeOne(966): 3
08-12 17:56:31.988: E/com.example.usemeone.UsemeOne(966): 4
08-12 17:56:31.998: E/com.example.usemeone.UsemeOne(966): 6
08-12 17:56:31.998: E/com.example.usemeone.UsemeOne(966): 9
08-12 17:56:32.157: E/com.example.usemeone.UsemeOne(966): AT :
08-12 17:56:32.398: E/com.example.usemeone.UsemeOne$2(966): 10
08-12 17:56:32.408: E/com.example.usemeone.UsemeOne$2(966): 11
08-12 17:56:32.408: E/com.example.usemeone.UsemeOne$SessionStatusCallback(966): OPENING
08-12 17:56:32.408: E/com.example.usemeone.UsemeOne$SessionStatusCallback(966): 13
08-12 17:57:26.468: E/com.example.usemeone.UsemeOne$SessionStatusCallback(966): CLOSED_LOGIN_FAILED
08-12 17:57:26.468: E/com.example.usemeone.UsemeOne$SessionStatusCallback(966): 13
它询问用户是否要允许该应用访问他/她的Facebook个人资料,但在授权该应用后,会话状态更改为CLOSED_LOGIN_FAILED。