No authentication challenges found while using twitter4j library

筅森魡賤 提交于 2019-11-30 09:08:18

问题


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 found exception in following scenario.

  1. I use App-only authentication (without loging into twitter)to fetch the public feeds from twitter. I fetch them successfully.
  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.
  3. But I get above mentioned exception while trying to login only, so I don't reach the reply or retweet.

Note: If I don't use App-only authentication and choose to login directly(without fetching public feed), then it works fine and I can login.

Following is the stacktrace:

11-14 19:33:03.597: W/System.err(3305): No authentication challenges found
11-14 19:33:03.667: W/System.err(3305): Relevant discussions can be found on the Internet at:
11-14 19:33:03.667: W/System.err(3305):     http://www.google.co.jp/search?q=9ddbeb3a or
11-14 19:33:03.727: W/System.err(3305):     http://www.google.co.jp/search?q=937eec8d
11-14 19:33:03.747: W/System.err(3305): TwitterException{exceptionCode=[9ddbeb3a-937eec8d c8a7b39b-dc5ea0d9], statusCode=-1, message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=3.0.4}
11-14 19:33:03.797: W/System.err(3305):     at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:177)
11-14 19:33:03.857: W/System.err(3305):     at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:61)
11-14 19:33:03.897: W/System.err(3305):     at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:98)
11-14 19:33:03.957: W/System.err(3305):     at twitter4j.auth.OAuthAuthorization.getOAuthAccessToken(OAuthAuthorization.java:145)
11-14 19:33:04.097: W/System.err(3305):     at twitter4j.auth.OAuthAuthorization.getOAuthAccessToken(OAuthAuthorization.java:165)
11-14 19:33:04.107: W/System.err(3305):     at twitter4j.TwitterBaseImpl.getOAuthAccessToken(TwitterBaseImpl.java:381)

EDIT:

Following is the code I am using to switch the configuration based on whether user is logged in or not.

private TwitterInstance(boolean withLoginConfig) {

        ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
        configurationBuilder
                .setOAuthConsumerKey(STAConstants.TWITTER_CONSUMER_KEY);
        configurationBuilder
                .setOAuthConsumerSecret(STAConstants.TWITTER_CONSUMER_SECRET);
        // configuration required to fetch feeds without login.
        if (!withLoginConfig) {
            configurationBuilder.setUseSSL(true);
            // IMPORTANT: set T4J to use App-only authentication
            configurationBuilder.setApplicationOnlyAuthEnabled(true);
            configurationBuilder.setOAuth2TokenType(getOAuth2Token()
                    .getTokenType());
            configurationBuilder.setOAuth2AccessToken(getOAuth2Token()
                    .getAccessToken());
        }
        Configuration configuration = configurationBuilder.build();
        twitterFactory = new TwitterFactory(configuration);
        twitter = twitterFactory.getInstance();
        requestToken = null;
    }

I searched for the solution on SO and other sites as well, but did not get any.

Any help is highly appreciated.


回答1:


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                           



回答2:


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"));


来源:https://stackoverflow.com/questions/19980197/no-authentication-challenges-found-while-using-twitter4j-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!