No authentication challenges found while using twitter4j library

前端 未结 2 705
谎友^
谎友^ 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                           
    
    0 讨论(0)
  • 2020-12-20 05:05

    After, version 1.1 of twitter api authentication is compulsory for all api calls , even for fetching public tweets and searching.. So, you have to add credentials by force:

     AccessToken accessToken = new AccessToken("ACCESS_TOKEN","TOKEN_SECRET");
     twitter.setOAuthAccessToken(accessToken);
    

    Full code can be as follows:

            ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
            configurationBuilder
                    .setOAuthConsumerKey(STAConstants.TWITTER_CONSUMER_KEY);
            configurationBuilder
                    .setOAuthConsumerSecret(STAConstants.TWITTER_CONSUMER_SECRET);
            Configuration configuration = configurationBuilder.build();
            TwitterFactory twitterFactory = new TwitterFactory(configuration);
            Twitter twitter = twitterFactory.getInstance();
            AccessToken accessToken = new AccessToken("ACCESS_TOKEN","TOKEN_SECRET");
            twitter.setOAuthAccessToken(accessToken);
            System.out.println(twitter.getUserTimeline("twitter"));
    
    0 讨论(0)
提交回复
热议问题