Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

method retrieveRequestToken raises "Communication with the service provider failed: null"

I am using twitter4j to send tweet from my application. When I invoke the method retrieveRequestToken, I get the error "Communication with the service provider failed: null".

public static void askOAuth(Context context) {
    try {
        // Use Apache HttpClient for HTTP messaging
        consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
        provider = new CommonsHttpOAuthProvider(
                "https://api.twitter.com/oauth/request_token",
                "https://api.twitter.com/oauth/access_token",
                "https://api.twitter.com/oauth/authorize");
        String authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL);
        Toast.makeText(context, "Authorize this app!", Toast.LENGTH_LONG).show();
        context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
    } catch (Exception e) {
        Log.e(APP, e.getMessage());
        Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
    }
}

Thank you.

like image 961
Guillermo Tobar Avatar asked Dec 16 '11 13:12

Guillermo Tobar


3 Answers

If this is in ICS, I highly recommend against using StrictMode.enableDefaults;.

ICS does not allow Http requests to occur in the UI thread, so when you do as above, you get that error.

To fix this, do provider.retrieveRequestToken(consumer, CALLBACK_URL); in a background thread as well as provider.retrieveAccessToken(consumer, verifier);.

Source: http://code.google.com/p/oauth-signpost/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Priority%20Milestone%20Owner%20Summary&groupby=&sort=&id=71

like image 135
Grantland Chew Avatar answered Nov 16 '22 01:11

Grantland Chew


I finally found the problem, he has to do with StrictModes in the latest versions of android. Running StrictMode.enableDefaults(); before making the call, the problem is corrected.

Although it worked for me, I would like to know if there is a more elegant solution to correct the problem.

like image 44
Guillermo Tobar Avatar answered Nov 16 '22 02:11

Guillermo Tobar


Have added permission tag regarding using "INTERNET" in android manifest file.

TAG ------>

Happy Coding... :-)

like image 1
Rocker Avatar answered Nov 16 '22 00:11

Rocker