Title: Twitter4J + Android: Authentication Challenge is Null Exception
Tags: android;twitter;oauth;twitter-oauth;twitter4j
Question: I'm using Twitter4J library for OAuth authentication but I get "Authentication Challenge is Null Exception" even before It opens the Twitter login page.
Here is the code.
Twitter twitter = new TwitterFactory().getInstance();
try
twitter.setOAuthConsumer(Startup.TWITTER_KEY, Startup.TWITTER_SECRET);
String callbackURL = "twitter-client:///";
RequestToken rToken = twitter.getOAuthRequestToken(callbackURL);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(rToken.getAuthenticationURL())));
catch(IllegalStateException e)
// access token is already available, or consumer key/secret is not set.
if(!twitter.getAuthorization().isEnabled()){
System.out.println("OAuth consumer key/secret is not set.");
System.exit(-1);
}
catch(Exception e)
Toast.makeText(Home.this, "Network Host not responding: "+e.getMessage(),Toast.LENGTH_SHORT).show();  //This exception.
Exception:
E/Home    ( 4393): Received
authentication challenge is null
E/Home    ( 4393): Received
authentication challenge is
nullRelevant discussions can be on the
Internet at: E/Home    ( 4393):
http://www.google.co.jp/search?q=6c607809
or E/Home    ( 4393):
http://www.google.co.jp/search?q=0f1d8134
E/Home    ( 4393):
TwitterException{exceptionCode=[6c607809-0f1d8134
cab4c0ac-d492a113], statusCode=-1,
retryAfter=0, rateLimitStatus=null,
version=2.2.2} E/Home    ( 4393):     at
twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:204)
E/Home    ( 4393):    at
twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:65)
E/Home    ( 4393):    at
twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:102)
E/Home    ( 4393):    at
twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:108)
E/Home    ( 4393):    at
twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:271)
E/Home    ( 4393):    at
com.sharj.trafik.view.Home.askOAuth(Home.java:157)
E/Home    ( 4393):    at
com.sharj.trafik.view.Home.access$0(Home.java:143)
E/Home    ( 4393):    at
com.sharj.trafik.view.Home$3.onClick(Home.java:110)
E/Home    ( 4393):    at
android.view.View.performClick(View.java:2485)
E/Home    ( 4393):    at
android.view.View$PerformClick.run(View.java:9080)
E/Home    ( 4393):    at
android.os.Handler.handleCallback(Handler.java:587)
E/Home    ( 4393):    at
android.os.Handler.dispatchMessage(Handler.java:92)
E/Home    ( 4393):    at
android.os.Looper.loop(Looper.java:130)
E/Home    ( 4393):    at
android.app.ActivityThread.main(ActivityThread.java:3683)
E/Home    ( 4393):    at
java.lang.reflect.Method.invokeNative(Native
Method) E/Home    ( 4393):    at
java.lang.reflect.Method.invoke(Method.java:507)
E/Home    ( 4393):    at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/Home    ( 4393):    at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/Home    ( 4393):    at
dalvik.system.NativeStart.main(Native
Method) E/Home    ( 4393): Caused by:
java.io.IOException: Received
authentication challenge is null
E/Home    ( 4393):    at
org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:1153)
E/Home    ( 4393):    at
org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:1095)
E/Home    ( 4393):    at
org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1048)
E/Home    ( 4393):    at
org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:726)
E/Home    ( 4393):    at
org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:121)
E/Home    ( 4393):    at
twitter4j.internal.http.HttpResponseImpl.(HttpResponseImpl.java:35)
E/Home    ( 4393):    at
twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:168)
E/Home    ( 4393):    ... 18 more
It contains following links, but Google doesn't have much on this.
http://www.google.co.jp/search?q=6c607809
http://www.google.co.jp/search?q=0f1d8134
Comment: Full stack trace would be helpful.

Comment: i'm having the same issue: did you ever get this solved? incorrect times didn't do it for me.

Comment: I faced this issue and problem was the time. My emulator time was not right

Comment: I have the same problem. How to fix it???? My datetime and timezone are correct.

Comment: No I wasn't able to solove it so I dropped the integration for a while. Will try again soon.

Here is the accepted answer: I had the same problem. It turned out that if the time stamp on the oAuth call is incorrect, the server returns a 401 status error which on Android devices causes the "Received authentication challenge is null" exception to be thrown.  All of the devices that were having this problem had incorrect times, and fixing the times fixed the problem.
Perhaps this is your issue as well?
Comment for this answer: Part of the oAuth protocol involves sending a time stamp with each command (this is probably handled by the class you are using).  There server compares the time stamp to it's current time, and if it does not fall within a certain window (for the server I am using it is 300 seconds), it will reject the command with a 401 error.  Unfortunately, Android does not handle the 401 error gracefully (this is a bug IMHO), and throws the exception that you are seeing.  If you set the time correctly, the problem goes away. (note that any other oAuth errors will also generate a 401 error).
Comment for this answer: http://stackoverflow.com/questions/20902350/android-twitter-received-authentication-challenge-is-null
Comment for this answer: Still facing same issue, please help me
Comment for this answer: This answer definitely needs more visibility!

Here is another answer: This happened for me when I called the following twice in a row:
requestToken = twitter.getOAuthRequestToken();
return requestToken.getAuthorizationURL();
Comment for this answer: I had the same issue... the TWICE was due to the User Cancelling OUT of the oAUTH the first time and then going back in a SECOND time to Authorize.  We were using the twitter.getSingleton "reusable" instance... across the calls... and that would NOT work.  We had to create the twitter instance each time using the ConfigurationBuilder

Here is another answer: I had same problem and fixed it by Using Asynchronous task
as I answered in the following link
Android Twitter exception error :(

Here is another answer: I too stumbled across this problem. Check my solution in another thread Android: Twitter4J Exception (Received Authentication Challenge Is Null)

Here is another answer: FIXED:  I had this exception even after I fixed the date&amp;time but when I looked in logcat at the params that twittherj sent to server the timestamp was still wrong - so I force-closed the app and after that exception was gone.

Here is another answer: I also had the same problem. First I was getting it when doing getOAuthRequestToken(). My consumer key and consumer secret were coming from .properties file, and turned out they had a trailing space, so were seen as invalid by Twitter's API.
After that, I also was getting the same 401 when doing getOAuthAccessToken(). Here, I wasn't correctly persisting and re-constructing RequestToken object.
Comment for this answer: have you got solution of this problem ?
i have used twitter4j-media-support-4.0.4 jar file

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 6, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2011:00:05/25/11', 'FavoriteCount': 3.0, 'PostTypeId': 1, 'Tags': '<android><twitter><oauth><twitter-oauth><twitter4j>', 'Title': 'Twitter4J + Android: Authentication Challenge is Null Exception', 'ViewCount': 8943, 'id': 6129316, 'user_AboutMe': '<p>Ruby on Rails / Android / iOS / Flutter</p>\n', 'user_AccountId': 138625.0, 'user_DisplayName': 'Sharjeel', 'user_Reputation': 15343.0, 'user_Views': 852.0, 'user_id': 343679.0}}