问题
I have created a Button in the UI which on clicking should call a function(fbLogin()), which opens a Session (if closed) and authorizes the user.
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);
}
}
});
}
}
My SessionStatusCallback class:
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();
}
}
In the SessionStatusCallback class it always shows the Session State first as OPENING and then CLOSED_LOGIN_FAILED. Why the the state is changing to CLOSED_LOGIN_FAILED.
Logcat Output:
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
Its asks the user if he wants to Allow the app to access his/her facebook profile, but after authorizing the app it changes session state to CLOSED_LOGIN_FAILED.
来源:https://stackoverflow.com/questions/18193614/not-being-able-to-authenticate-using-in-android-app