I use twitter4j in my android application. I can get oauth_verifier and fetch token and secret ( with accessToken.getToken() and accessToken.getTokenSecret() ). I store those value.
However,I don't know how to use this value when the app open again.I found something like this.
accessToken = this.twitter.getOAuthAccessToken(requestToken, verifier);
but how can I create requestToken?
when you got the token and the token secret save them in your app , example using SharedPreference
AccessToken accessToken = twitter.getOAuthAccessToken(oathVerifier);
SharedPreferences pref= getSharedPreferences("social_pref",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("token",accessToken.getToken());
editor.putString("token_secret",accessToken.getTokenSecret());
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.GINGERBREAD) {
editor.apply();
}else{
editor.commit();
}
Now you can easily know if there is a stored token and token secret
public boolean doIhaveTokenAndTokenSecret(Context context){
SharedPreferences pref= getSharedPreferences("social_pref",Context.MODE_PRIVATE);
return pref.getString("token",null)!=null && pref.getString("token_secret",null)!=null ;
}
if you have them stored provide them to ConfigurationBuilder object
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(Constants.TWITTER_CONSUMER_KEY);
builder.setOAuthConsumerSecret(Constants.TWITTER_CONSUMER_SECRET);
if(doIhaveTokenAndTokenSecret(getApplicationContext()){
SharedPreferences mSharePref = context.getSharedPreferences(Constants.SOCIAL_PREF_NAME, Context.MODE_PRIVATE);
builder.setOAuthAccessToken(mSharePref.getString("token",null));
builder.setOAuthAccessTokenSecret(mSharePref.getString("token_secret",null));
}
Configuration configuration = builder.build();
Twitter mTwitter = new TwitterFactory(configuration).getInstance();