Twitter Kit Android - constantly asks for authentication


#1

have a twitter login file which is pretty boiler plate:

public class TwitterLogin extends AppCompatActivity {
TwitterAuthClient client;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_twitter_login);
    TwitterConfig config = new TwitterConfig.Builder(this)
            .logger(new DefaultLogger(Log.DEBUG))
            .twitterAuthConfig(new TwitterAuthConfig("key", "key"))
            .debug(true)
            .build();
    Twitter.initialize(config);
    client = new TwitterAuthClient();
    //make the call to login
    client.authorize(this, new Callback<TwitterSession>() {
        @Override
        public void success(Result<TwitterSession> result) {
            //feedback
            Toast.makeText(getApplicationContext(), "Login worked", Toast.LENGTH_LONG).show();
        }

        @Override
        public void failure(TwitterException e) {
            //feedback
            Toast.makeText(getApplicationContext(), "Login failed", Toast.LENGTH_LONG).show();
        }
    });
    startActivity(new Intent(TwitterLogin.this,MainFeedScreen.class));


}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //this method call is necessary to get our callback to get called.
    client.onActivityResult(requestCode, resultCode, data);


}

}
This seems to work fine.

However, I also have an AsyncTask class that is meant to go off and get a users home timeline:


public class TwitterRequests extends AsyncTask<String,String,JSONObject> {`
  TwitterApiClient twitterApiClient;
       StatusesService statusesService;
        private MainFeedScreen mainAct;
        private ProgressDialog progressDialog;
       private List<Tweet> tweetList; 
   public TwitterRequests(View v, MainFeedScreen mainAct){
        twitterApiClient = TwitterCore.getInstance().getApiClient();
        statusesService = twitterApiClient.getStatusesService();
        this.mainAct = mainAct;
        this.progressDialog = new ProgressDialog(v.getContext());
}

@Override
protected JSONObject doInBackground(String... params) {
    Call<List<Tweet>> call  = statusesService.homeTimeline(20,null,null,false,false,true,true);
    call.enqueue(new Callback<List<Tweet>>() {
        @Override
        public void onResponse(Call<List<Tweet>> call, Response<List<Tweet>> response) {
            Log.d("Twitter","In on response");

              //            tweetList = response.body();
        }

        @Override
        public void onFailure(Call<List<Tweet>> call, Throwable t) {
            Log.d("Twitter","in failure");



        }
    });


    return new JSONObject();
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    progressDialog.show();
}

@Override
protected void onPostExecute(JSONObject jsonObject) {
    super.onPostExecute(jsonObject);
    mainAct.setRedditAdapter(new TwitterAdapter(tweetList));
    mainAct.getmRecyclerView().setAdapter(mainAct.getRedditAdapter());
    progressDialog.hide();
 }
}`

In another activity I make a new instance of TwitterRequest() and execute it.

Whenever I do this, I am redirected to a webview for Twitter where I must authorise myself again. If i press authorise it redirects me to my homescreen. If i press the button again it does the same thing.

Any ideas why I keep needing to reauthorise my app?

many thanks.


#2

You can check to see if user has already been authenticated and skip the login.

if (TwitterCore.getInstance().getSessionManager().getActiveSession() != null) {
    // do something else
}

#3

Thanks for the response, but this doesn’t really help. The code tells me that login was successful but still redirects me to login again as soon as i tap twitter again.


#4

I also have same problem with here but I can’t use this solution, the problem is I use TwitterAuthClient only when I want to share to Twitter so there’s no logout button. If I use your solution, the same account will stuck forever there.
For information, I only set Read and Write permission without DM.
This is what I found on internet and other discussion:
there are 2 different type: Authorize and Authenticate.
Authorize will always ask for permission, and Authenticate only ask once. but in TwitterAuthClient only have Authorize(). how can I access Authenticate in Android?