No authentication challenges found while using twitter4j library

前端 未结 2 720
谎友^
谎友^ 2020-12-20 04:11

I am currently working on integrating Twitter in my android application. I integrated it successfully to an extent. However I get the No authentication challenges foun

2条回答
  •  暖寄归人
    2020-12-20 04:58

    I worked on few things and finally found answer myself to my question. Hence posting here.

    Issue: No authentication challenges found

    Reason : I was not sending AccessToken and AccessTokenSecret in configuration after Login. Hence it was not getting the authentication data.

    Solution :

    Step 1: I use App-only authentication (without loging into twitter) to fetch the public feeds from twitter. I fetch them successfully. For this I use BearerToken in configuration as follows:

       ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
       configurationBuilder.setOAuthConsumerKey(Constants.TWITTER_CONSUMER_KEY);
       configurationBuilder.setOAuthConsumerSecret(Constants.TWITTER_CONSUMER_SECRET);
       configurationBuilder.setUseSSL(true);
       // IMPORTANT: set T4J to use App-only authentication
       configurationBuilder.setApplicationOnlyAuthEnabled(true);
       configurationBuilder.setOAuth2TokenType(getOAuth2Token().getTokenType());
       configurationBuilder.setOAuth2AccessToken(getOAuth2Token().getAccessToken());
    

    Step 2: When I try to reply or retweet or mark it as favorite, I check if user is already logged in or not, if it is not logged-in then I try to login first and then do the reply or retweet. For this I use following configuration.

    ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
    configurationBuilder.setOAuthConsumerKey(Constants.TWITTER_CONSUMER_KEY);
    configurationBuilder.setOAuthConsumerSecret(Constants.TWITTER_CONSUMER_SECRET);
    

    This will give me RequestToken which in turn will give me the AccessToken and AccessTokenSecret.

    Step 3 : Thereafter I use following configuration for subsequent requests without any problem.

    ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
    configurationBuilder.setOAuthConsumerKey(Constants.TWITTER_CONSUMER_KEY);
    configurationBuilder.setOAuthConsumerSecret(Constants.TWITTER_CONSUMER_SECRET);
    configurationBuilder.setOAuthAccessToken(getAccessToken()); // from sharedPreferences
    configurationBuilder.setOAuthAccessTokenSecret(getAccessTokenSecret()); // from sharedPreferences                           
    

提交回复
热议问题