问题
I'm trying to implement Facebook login on Android, using the Facebook API. I've done everything the way Facebook Login Flow says. The code is below.
public class FBSignIn extends Fragment {
private UiLifecycleHelper uiHelper;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = super.onCreateView(inflater, container, savedInstanceState);
setTitle("Sign In");
reloadTemplate();
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
return v;
}
@Override
public void onResume() {
super.onResume();
uiHelper.onResume();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
@Override
public boolean executeCommand(BaseWebView webView, String command, String data) {
if (command.equalsIgnoreCase("login")) {
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
loginWithFBToken(session.getAccessToken());
return;
}
Session.openActiveSession(getActivity(), true, callback);
}
return true;
}
private Session.StatusCallback callback = new Session.StatusCallback() {
@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()) {
// Facebook logged in...
} else if (state.isClosed()) {
//Facebook logged out...
}
}
}
However, after the Session.openActiveSession() call, in onSessionsStateChange() function, I first get state == CLOSED_LOGIN_FAILED; after that it's called the second time, with state == OPENING. So it never gets into an OPENED state.
What's wrong with this code? I'd very much appreciate your help.
P.S. I've commented out everything concerning uiHelper and, in my main activity, supplied the overriden method:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
Now the first call to onSessionStateChange comes with state == OPENING and exception == null. The 2nd call comes with state == CLOSED_LOGIN_FAILED and exception == Invalid access token. What does that mean?
回答1:
I've solved the issue. The reason was that our app was registered in Facebook in Sandbox mode, and only certain users could log in with Facebook. When I logged in under one of these accounts, it worked.
来源:https://stackoverflow.com/questions/18699707/facebook-api-cannot-login-on-android