我正在關注this tutorial。我儘可能地從savedPreferences
中檢索訪問令牌,然後創建一個註銷按鈕。問題是,似乎我access_token永遠是正確的,無論我從我的應用程序,甚至從Facebook應用程序註銷。Facebook Android教程註銷
這是我的代碼:
public class MainActivity extends Activity {
private String TAG = MainActivity.class.getName();
Facebook mFacebook = new Facebook("whatever");
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(mFacebook);
private SharedPreferences mPrefs;
//UI elements
private ImageButton fbLoginButton;
private Button getInfoButton;
private Button fbLogoutButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPrefs = getPreferences(MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if(access_token != null) {
Log.d(TAG, "Token found. Setting token " + access_token);
mFacebook.setAccessToken(access_token);
}
if(expires != 0) {
Log.d(TAG, "Setting expire " + expires);
mFacebook.setAccessExpires(expires);
}
if(!mFacebook.isSessionValid()) {
mFacebook.authorize(this, new String[] {"email", "read_friendlists" }, new DialogListener() {
@Override
public void onComplete(Bundle values) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token", mFacebook.getAccessToken());
editor.putLong("access_expires", mFacebook.getAccessExpires());
editor.commit();
Log.d(TAG, "Logging you in");
}
@Override
public void onFacebookError(FacebookError error) {
Log.d(TAG, "Facebook error: " + error.getMessage());
}
@Override
public void onError(DialogError e) {
Log.d(TAG, "Error: " + e.getMessage());
}
@Override
public void onCancel() {
Log.d(TAG, "User Canceled");
}
});
}
else{
Toast.makeText(this, "Already logged in", Toast.LENGTH_LONG).show();
}
fbLoginButton = (ImageButton) findViewById(R.id.buttonFBLogin);
getInfoButton = (Button) findViewById(R.id.buttonGetInfo);
fbLogoutButton = (Button) findViewById(R.id.buttonLogout);
fbLogoutButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
doLogout();
}
});
}
protected void doLogout() {
mAsyncRunner.logout(this, new RequestListener() {
@Override
public void onComplete(String response, Object state) {
Log.d(TAG, "Logging you out");
}
@Override
public void onIOException(IOException e, Object state) {}
@Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {}
@Override
public void onMalformedURLException(MalformedURLException e,
Object state) {}
@Override
public void onFacebookError(FacebookError e, Object state) {}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mFacebook.authorizeCallback(requestCode, resultCode, data);
}
}
我總是得到Already logged in
Toast.Am我做錯了什麼?
PS。在從sharedpreferences
檢索access_token之前,該應用程序似乎工作正常,所以它不是我的API密鑰或調試密鑰的問題。
嘗試將一些日誌消息放入'mAsyncRunner.logout'內的不同函數中,以查看您是否遇到某種錯誤 –
@Kelmer您是否解決了此問題? Iam也面臨同樣的問題Iam沒有得到任何解決方案。 – Mano