我有我的主要活動一些代碼,調用我的Facebook登錄活動類,如果AccessToken.getCurrentAccessToken()爲null:Facebook的訪問令牌總是空
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;
import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.Profile;
import com.facebook.ProfileTracker;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import java.util.Arrays;
import matthewboyle.lurker_android.MainFeedScreen;
import matthewboyle.lurker_android.utilities.ConnectionChecker;
/**
* Created by matthewboyle on 28/05/2017.
*/
public class FacebookLogin extends AppCompatActivity {
private CallbackManager mCallbackManager;
private AccessTokenTracker accessTokenTracker;
private ProfileTracker profileTracker;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCallbackManager = CallbackManager.Factory.create();
accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
Log.d("facebook", "onCurrentAccessTokenChanged");
}
};
profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
Log.d("facebook", "onCurrentProfileChanged");
}
};
accessTokenTracker.startTracking();
profileTracker.startTracking();
LoginManager.getInstance().registerCallback(mCallbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d("facebook", "in on success");
AccessToken accessToken = loginResult.getAccessToken();
Log.d("permissions", accessToken.getPermissions().toString());
Log.d("facebook", "in on success,got a token and its "+accessToken);
}
@Override
public void onCancel() {
Log.d("facebook", "in cancel");
}
@Override
public void onError(FacebookException exception) {
Log.d("facebook", "in error");
Log.d("facebook", exception.toString());
}
});
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_posts","user_likes","user_about_me","user_managed_groups","user_tagged_places"));
Log.d("facebook", "Done so redirecting with "+AccessToken.getCurrentAccessToken());
startActivity(new Intent(FacebookLogin.this,MainFeedScreen.class));
}
@Override
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
super.onActivityResult(requestCode, responseCode, intent);
//Facebook login
mCallbackManager.onActivityResult(requestCode, responseCode, intent);
}
}
如果我運行此代碼,唯一的打印語句我得到的是:
Done so redirecting with null
它似乎沒有擊中任何其他類的方法。
值得一提的是,此代碼工作正常,直到我重新安裝應用程序。我去了Facebook並在我的應用中更新了哈希鍵,但這似乎沒有幫助。但是,我沒有看到任何由於散列鍵造成的錯誤。
希望有任何幫助。
Dura是正確的。你必須移除'startActivity(新的Intent(FacebookLogin.this,MainFeedScreen.class));'或者將它移動到'onSuccess'上。 – Codus