我在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(), "13");
// updateView();
}
}
但是,当我单击“登录”按钮时,活动将继续加载(就像它正在获取令牌一样),但会话永远不会打开(因为记录器从未在LogCat中记录15)
LogCat输出:
08-12 16:11:35.836: E/com.example.usemeone.UsemeOne(1725): 0
08-12 16:11:35.846: E/com.example.usemeone.UsemeOne(1725): 1
08-12 16:11:35.846: E/com.example.usemeone.UsemeOne(1725): 3
08-12 16:11:35.886: E/com.example.usemeone.UsemeOne(1725): 4
08-12 16:11:35.896: E/com.example.usemeone.UsemeOne(1725): 6
08-12 16:11:35.896: E/com.example.usemeone.UsemeOne(1725): 9
08-12 16:11:36.046: E/com.example.usemeone.UsemeOne(1725): AT :
08-12 16:11:36.366: E/com.example.usemeone.UsemeOne$SessionStatusCallback(1725): 13
08-12 16:11:37.196: E/com.example.usemeone.UsemeOne$2(1725): 10
08-12 16:11:37.196: E/com.example.usemeone.UsemeOne$2(1725): 11
答案 0 :(得分:0)
您是否正在为onActivityResult()
实施Session
?否则它将无法完成身份验证。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Session.DEFAULT_AUTHORIZE_ACTIVITY_CODE) {
super.onActivityResult(requestCode, resultCode, data);
if (Session.getActiveSession() != null)
Session.getActiveSession().onActivityResult(this, requestCode,
resultCode, data);
Session currentSession = Session.getActiveSession();
if (currentSession == null || currentSession.getState().isClosed()) {
Session session = new Session.Builder(this).build();
Session.setActiveSession(session);
currentSession = session;
}
if (currentSession.isOpened()) {
Session.openActiveSession(this, true, statusCallback);
}
}
}