问题
I try this code to perform login to my app using fb.
public class FacebookLogin2Activity extends BaseActivity {
private String TAG = "MainActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.facebook_login);
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
fetchUserDetails(session);
}
LoginButton authButton = (LoginButton) findViewById(R.id.authButton);
authButton.setOnErrorListener(new OnErrorListener() {
@Override
public void onError(FacebookException error) {
Log.e(TAG, "Error " + error.getMessage());
}
});
// set permission list, Don't forget to add email
authButton.setReadPermissions(Arrays.asList("basic_info", "email",
"birthday"));
// option A
// Session.openActiveSession(this, true, new Session.StatusCallback() {
//
// // callback when session changes state
// @Override
// public void call(Session session, SessionState state,
// Exception exception) {
// fetchUserDetails(session);
// }
// });
// // option B
// session state call back event
authButton.setSessionStatusCallback(new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state,
Exception exception) {
if (session.isOpened()) {
fetchUserDetails(session);
}
}
});
}
private void fetchUserDetails(Session session) {
Log.i(TAG, "Access Token" + session.getAccessToken());
Request.newMeRequest(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
//some code
}
}
}).executeAsync();
}
but i get this error
what should i check?
{Session state:CLOSED_LOGIN_FAILED, token:{AccessToken token:ACCESS_TOKEN_REMOVED permissions:[]}, appId:131390430364547}
and I have verified Facebook developers has my android hash code in the website.
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// start Facebook Login
Session.openActiveSession(this, true, new Session.StatusCallback() {
// callback when session changes state
@Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
// make request to the /me API
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
TextView welcome = (TextView) findViewById(R.id.welcome);
welcome.setText("Hello " + user.getName() + "!");
}
}
});
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
}
回答1:
check your Manifest.xml
<meta-data android:value="@string/facebook_app_id" android:name="com.facebook.sdk.ApplicationId"/>
and your strings.xml have
<string name="facebook_app_id">131390430364547</string>
verify the app_id is the same in your facebook app configuration and set the "yourApp.mainactivity" in the Class field
is it the debbug app or released app? the hash code is diferent for each one
Edited *
for verify the status of the session
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
if (session.isOpened()) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Log.e("user", "session established");
Request.newMeRequest(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
Log.e("user", "loged user");
buildUserInfoDisplay(user);
}
}
}).executeAsync();
}
else{
Log.e("user", "session not established");
}
}
private void onSessionStateChange(Session session, SessionState state,
Exception exception) {
}
};
and in the on create method to use the login button
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
setContentView(R.layout.login);
buttonLoginLogout = (LoginButton)findViewById(R.id.authButton);
buttonLoginLogout.setReadPermissions(Arrays.asList("user_status"));
}
}
also need to manage the ui status in al lifecycle methods
public void onResume() {
super.onResume();
uiHelper.onResume();
}
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
I have an app with this login exactly as i posted it, it wors good, facebook developer console is tracking the sessions of users. Try it !
回答2:
There could be many possible reasons:
Wrong key hash. Generate one from the code provided by facebook:
PackageInfo info = getPackageManager().getPackageInfo("<your_package_name>", PackageManager.GET_SIGNATURES); for (Signature signature : info.signatures){ MessageDigest md = MessageDigest.getInstance("SHA"); md.update(signature.toByteArray()); Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT)); }Make sure to replace
"<your_package_name>"with your corresponding package name. Look at logcat and grab the keyhash and enter it in your facebook app settings.Open the Facebook native app and make sure you are logged in properly and can access content. If you have incorrect credentials entered in the Facebook native app (perhaps you recently changed your password) then the Facebook SDK will repeatedly try to do an SSO using the native app and report back
CLOSED_LOGIN_FAILED.If you are using the Facebook application in the sandbox mode, make sure the account you are using is in the list of authorized admins or developers.
来源:https://stackoverflow.com/questions/23046136/facebook-login-fails-session-stateclosed-login-failed-tokenaccesstoken-tok