Twitter4j getOAuthRequestToken() with callback URL

2020-07-19 04:53发布

问题:

I am trying to integrate Twitter into my Android Application and can't seem to get Twitter4j to register my app.

Every tutorial and question regarding the issue I've seen for Android have been years old and things seem to have moved on since then.

I followed the steps on this tutorial and used the supplied source code to test connecting to Twitter. Knowing that the Twitter4j version supplied with the source code was out of date I updated it the latest version and snapshot version later on, but all to no avail.

private void loginNewUser() {
    try {
        Log.i(TAG, "Request App Authentication");

        // This line fails with error "Host is unresolved: api.twitter.com:80
        mReqToken = mTwitter.getOAuthRequestToken(); 

        Log.i(TAG, "Starting Webview to login to twitter");
        WebView twitterSite = new WebView(this);
        twitterSite.loadUrl(mReqToken.getAuthenticationURL());
        setContentView(twitterSite);

    } catch (TwitterException e) {
        Log.e("errors", e.getMessage());
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
    }
}

Of anyone has any working login code using Twitter4j or knows a solution to the problem I'd be grateful to hear from you!

回答1:

Okay I figured it out and I hope anyone who comes across the same problem doesn't have to suffer as I did.

1) The error "Host is unresolved: api.twitter.com:80" comes from the fact that the device did not have a WiFi signal or 3G connectivity (The WiFi is a bit flaky in the office I work in).

2) getOAuthRequestToken() crashes in any Android version greater than 3.0 Honeycomb because HTTP requests need to be done on a separate thread from the UI thread. I had this problem because I had set the target version in my Android manifest to 4.0 Ice Cream Sandwich.



回答2:

I had this problem too and the reason I couldn't log in to Twitter is that I modified the time and date of my device for making tests.

Once that I put the right date and time, the login to Twitter worked.

Hope it helps anybody.