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.
App-only authentication
(without loging into twitter)to fetch the public feeds from twitter. I fetch them successfully.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.
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