Error with Twitter4J from Android with browser user authentication: No authentication challenges found


#1

I am trying to develop my own Android application using Android Studio 0.4.2 and the Twitter4J library. My idea is to use a WebView to load there the Twitter authentication page and get the AccessToken from there. I can get the oauth_token and oauth_verifier, but after that all the Twitter methods to get followers, post twits, whatever are not working.

This is my code:

public class TwitterLoginFragment extends Fragment {
private static String TWITTER_CONSUMER_KEY = “***”;
private static String TWITTER_CONSUMER_SECRET = “***”;
private static final String TWITTER_CALLBACK_URL = “http://www.hita.pro”;

private static SharedPreferences sharedPreferences;
private Button btnTwitterLogin;
private Button btnTwitterLogOut;
private WebView wvTwitterLogin;

private IDs iDs;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    sharedPreferences = getActivity().getSharedPreferences("CONNECTION_INFO", Context.MODE_PRIVATE);
    TwitterFactory.getSingleton().setOAuthConsumer(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
    return inflater.inflate(R.layout.fragment_twitter_login, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstance) {
    super.onActivityCreated(savedInstance);

    wvTwitterLogin = (WebView) getView().findViewById(R.id.wvTwitterLogin);
    wvTwitterLogin.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean("KEY_TWITTER_LOGIN", true);
            editor.putString("OAUTH_TOKEN", url.substring(url.indexOf("oauth_token=") + 12, url.indexOf("&")));
            editor.putString("OAUTH_VERIFIER", url.substring(url.indexOf("oauth_verifier=") + 15));
            editor.commit();

            TwitterFactory.getSingleton().setOAuthAccessToken(new AccessToken(sharedPreferences.getString("OAUTH_TOKEN", ""), sharedPreferences.getString("OAUTH_VERIFIER", "")));

            new GetTwitterFollowers().execute();

            return true;
        }
    });

    btnTwitterLogin = (Button) getView().findViewById(R.id.btnTwitterLogin);
    btnTwitterLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            loginToTwitter();
        }
    });

    btnTwitterLogOut = (Button) getView().findViewById(R.id.btnTwitterLogOut);
    btnTwitterLogOut.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            twitterLogOut();
        }
    });
}

public void loginToTwitter() {
    if (!isTwitterLoggedInAlready()) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    wvTwitterLogin.loadUrl(TwitterFactory.getSingleton().getOAuthRequestToken(TWITTER_CALLBACK_URL).getAuthenticationURL());

                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            goToTwitterLogin();
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(getActivity(), getString(R.string.error_already_logged_twitter), Toast.LENGTH_LONG).show();
                }
            }
        });

        thread.start();
    } else
        Toast.makeText(getActivity(), getString(R.string.error_already_logged_twitter), Toast.LENGTH_LONG).show();
}

private void goToTwitterLogin() {
    btnTwitterLogin.setVisibility(View.GONE);
    btnTwitterLogOut.setVisibility(View.GONE);
    wvTwitterLogin.setVisibility(View.VISIBLE);
}

private boolean isTwitterLoggedInAlready() {
    return sharedPreferences.getBoolean("KEY_TWITTER_LOGIN", false);
}

private void twitterLogOut() {
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("KEY_TWITTER_LOGIN", false);
    editor.commit();
}

private class GetTwitterFollowers extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... v) {
        try {
            iDs = TwitterFactory.getSingleton().getFollowersIDs(-1);
        } catch (TwitterException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        iDs.getIDs();
    }
}

}

When I call to TwitterFactory.getSingleton().getFollowersIDs(-1) after the user has singed in, I’m getting this exception:

java.io.IOException: No authentication challenges found

I’m desperated, I have spent more than one week trying to solve this problem but all the threads I have found here and other sites are not working. The system clock is OK, I have tried with ConfigurationBuilder and other solutions, but no luck. Can somebody help me?

Thanks a lot!


#2

is nobody having this stuff? is working for everybody excepts for me?


#3

I can’t beleive nobody is having this problem, then, is nobody using Twitter4J? Which API are people using for communication between Twitter and Android?


#4

i am having same problem. help me


#5

i also have same problem on twitter4j jar.

06-24 00:28:22.463: I/TWITTER LOGIN ERROR(5951): No authentication challenges found
06-24 00:28:22.463: I/TWITTER LOGIN ERROR(5951): Relevant discussions can be found on the Internet at:
06-24 00:28:22.463: I/TWITTER LOGIN ERROR(5951): http://www.google.co.jp/search?q=10f5ada3 or
06-24 00:28:22.463: I/TWITTER LOGIN ERROR(5951): http://www.google.co.jp/search?q=0276a2ba
06-24 00:28:22.463: I/TWITTER LOGIN ERROR(5951): TwitterException{exceptionCode=[10f5ada3-0276a2ba b8c63e52-4c5db04c], statusCode=-1, message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=3.0.5}


#6

I am also facing the type of problem
W/System.err(3396): java.io.IOException: No authentication challenges found
at libcore.net.http.HttpURLConnectionImpl.getAuthorizationCredentials(HttpURLConnectionImpl.java:427)
at libcore.net.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:407)