I am trying to post tweets on twitter using twitter integration.My project works fine for the first time while posting tweet and tweet is successfully posted but shows below popup at second time .If i close the fragment and then start again then it works fine but without closing if i try to post tweet for second time it shows popup. Logcat shows-
https://api.twitter.com/oauth/authorize?oauth_token=xyz
Popup shows-
![enter image description here][1]
The issue might be beause second time request token cannot be accessed..I have used following code for accessing request token..
protected String doInBackground(String... args) {
try {
if(requestTokenFirstTime) {
requestToken = twitter.getOAuthRequestToken();
requestTokenFirstTime = false;
SharedPreferences.Editor edit = pref.edit();
edit.putString("Request_TOKEN", requestToken.getToken());
edit.putString("Request_TOKEN_SECRET", requestToken.getTokenSecret());
edit.commit();
requestTokenFirstTime = false;
}
else {
requestToken = new RequestToken(pref.getString("Request_TOKEN", ""), pref.getString("Request_TOKEN_SECRET", ""));
}
oauth_url = requestToken.getAuthorizationURL();
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return oauth_url;
}
If i remove else part and try to request request token each time then app crashes displaying following errors saying "Access token already available..
I tried invalidating accessToken by putting
twitter.setOAuthAccessToken(null);
twitter.setOAuth2Token(null);
inside onPostExecute() but that also didn't worked..
This solved my problem..
private class LoginProcess implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (requestTokenFirstTime)
{
new TokenGet().execute();
}
else
{
Fragment profile = new ProfileFragment();
FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
ft.replace(R.id.frame_container, profile);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
}
}
private class TokenGet extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... args) {
try {
requestToken = twitter.getOAuthRequestToken();
requestTokenFirstTime = false;
SharedPreferences.Editor edit = pref.edit();
edit.putString("Request_TOKEN", requestToken.getToken());
edit.putString("Request_TOKEN_SECRET", requestToken.getTokenSecret());
edit.commit();
requestTokenFirstTime = false;
oauth_url = requestToken.getAuthorizationURL();
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return oauth_url;
}