I figured it out
I’m trying to create my own android twitter client and i previously managed to use the Twitter Kit to login and fetch the Home Timeline but after implementing a logout function logging in doesn’t work anymore. I deleted everything related to logging out and left it as it was before and still, not working. I found out that even though the login succeeds but the success method is never called.
Here is my code and the logs:
Main Activity
public class MainActivity extends AppCompatActivity {
TwitterLoginButton loginButton;
String token;
String secret;
TwitterAuthToken authToken;
Long userId;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Pass the activity result to the login button
loginButton.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TwitterConfig config = new TwitterConfig.Builder(this)
.logger(new DefaultLogger(Log.DEBUG))
.twitterAuthConfig(new TwitterAuthConfig(CONSUMER_KEY, CONSUMER_SECRET))
.debug(true)
.build();
Twitter.initialize(config);
setContentView(R.layout.activity_main);
final SharedPreferences loginPrefs = getPreferences(MODE_PRIVATE);
prefLogin();
loginButton = findViewById(R.id.login_button);
loginButton.setCallback(new Callback() {
@Override
public void success(Result result) {
// If the login goes well
TwitterSession session = TwitterCore.getInstance().getSessionManager().getActiveSession();
authToken = session.getAuthToken();
token = authToken.token;
secret = authToken.secret;
userId = session.getUserId();
String username = result.data.getUserName();
loginPrefs.edit().putString("token", token).apply();
loginPrefs.edit().putString("secret", secret).apply();
loginPrefs.edit().putLong("userid", userId).apply();
loginPrefs.edit().putString("username", username).apply();
login(username);
}
private void login(String username) {
Intent intent = new Intent(getApplicationContext(), Timeline.class);
intent.putExtra("username", username);
startActivity(intent);
}
@Override
public void failure(TwitterException exception) {
// if the login fails
Toast.makeText(MainActivity.this, "Login failed", Toast.LENGTH_SHORT).show();
}
});
}
private void prefLogin() {
SharedPreferences loginPrefs = getPreferences(MODE_PRIVATE);
String preftoken = loginPrefs.getString("token", null);
String prefsecret = loginPrefs.getString("secret", null);
Long prefuserId = loginPrefs.getLong("userid", 0);
String prefusername = loginPrefs.getString("username", null);
if (!(preftoken == null) && !(prefsecret == null) && !(prefuserId == 0) && !(prefusername == null)) {
TwitterAuthToken twitterAuthToken = new TwitterAuthToken(preftoken, prefsecret);
TwitterSession twitterSession = new TwitterSession(twitterAuthToken, prefuserId, prefusername);
SessionManager sessionManager = TwitterCore.getInstance().getSessionManager();
sessionManager.setActiveSession(twitterSession);
Intent intent = new Intent(getApplicationContext(), Timeline.class);
startActivity(intent);
}
}
@Override
protected void onPause() {
super.onPause();
finish();
}
}
Logs
11-27 09:58:04.898 2770-2770/com.dinud11.dinudtwitter D/Twitter: Using OAuth
11-27 09:58:05.421 2770-2770/com.dinud11.dinudtwitter D/Twitter: Obtaining
request token to start the sign in flow
11-27 09:58:05.886 2770-2770/com.dinud11.dinudtwitter D/Twitter: Redirecting
user to web view to complete authorization flow
11-27 09:58:08.599 2770-2770/com.dinud11.dinudtwitter D/Twitter: OAuth web
view completed successfully
11-27 09:58:08.599 2770-2770/com.dinud11.dinudtwitter D/Twitter: Converting
the request token to an access token.
I’m sorry if this has been asked before but I couldn’t find anything.