1

Well, I'm trying to get Twitter to work on a basic Android application, it works just fine in my 2.3.6 but as I try to use 4.0+ it fails trying to authenticate.. Here is my small little code

private OAuthProvider provider = new DefaultOAuthProvider(REQUEST_URL, ACCESS_URL, AUTHORIZE_URL);
private CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
protected void onResume() {
    super.onResume();
    WebView webview = new WebView(this);
    webview.getSettings().setJavaScriptEnabled(true);  
    webview.setVisibility(View.VISIBLE);
    setContentView(webview);
    try {
        String authUrl = provider.retrieveRequestToken(consumer, OAUTH_CALLBACK_URL); 
        webview.setWebViewClient(new WebViewClient() {  
            @Override  
            public void onPageFinished(WebView view, String url)  {  
                Uri uri = Uri.parse(url);
                if (uri != null && uri.toString().startsWith(OAUTH_CALLBACK_URL)) {
                    view.setVisibility(View.INVISIBLE);
                    String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
                    try {
                        provider.retrieveAccessToken(consumer, verifier);
                        String claveUsuario = consumer.getToken();
                        String secretoUsuario = consumer.getTokenSecret();
                        HttpParameters params1 = provider.getResponseParameters();
                        String userName = params1.getFirst("screen_name");
                        SharedPreferences settings = getBaseContext().getSharedPreferences(PREFERENCIAS, 0);
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putString("clave_usuario", claveUsuario);
                        editor.putString("secreto_usuario", secretoUsuario);
                        editor.putString("nombre_usuario", userName);
                        editor.commit();
                        Intent intent = getIntent();
                        String origen = intent.getExtras().getString("Origen");
                        if (origen != null) {
                            if (origen.equals("Twittear")) {
                                startActivity(new Intent(AutenticarTwitter.this, Twittear.class));
                                finish();
                            }
                            else if (origen.equals("Autores")) {
                                startActivity(new Intent(AutenticarTwitter.this, Autores.class));
                                finish();
                            }
                        }
                        else {
                            startActivity(new Intent(AutenticarTwitter.this, Ajustes.class));
                            finish();
                        }
                    } 
                    catch (Exception e) {}                
                }
            }   
        });  
        webview.loadUrl(authUrl);
    } 
    catch (Exception ex) {}
}

And as I said, it works just fine in Android 2.3.6, but when it comes to 4.0+ it fails in line:

String authUrl = provider.retrieveRequestToken(consumer, OAUTH_CALLBACK_URL); 

And I've unsuccesfully tried to fix this. My logcat is something like:

04-29 22:37:39.256: W/System.err(784): oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: null
04-29 22:37:39.256: W/System.err(784):  at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java:214)
04-29 22:37:39.256: W/System.err(784):  at oauth.signpost.AbstractOAuthProvider.retrieveRequestToken(AbstractOAuthProvider.java:69)
04-29 22:37:39.256: W/System.err(784):  at com.example.twitter.OAuthAccessTokenActivity.onResume(OAuthAccessTokenActivity.java:43)
04-29 22:37:39.256: W/System.err(784):  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1185)
04-29 22:37:39.276: W/System.err(784):  at android.app.Activity.performResume(Activity.java:5182)
04-29 22:37:39.276: W/System.err(784):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2732)
04-29 22:37:39.276: W/System.err(784):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2771)
04-29 22:37:39.276: W/System.err(784):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2235)
04-29 22:37:39.296: W/System.err(784):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-29 22:37:39.296: W/System.err(784):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-29 22:37:39.296: W/System.err(784):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-29 22:37:39.296: W/System.err(784):  at android.os.Looper.loop(Looper.java:137)
royhowie
  • 11,075
  • 14
  • 50
  • 67
Jorge
  • 1,574
  • 2
  • 12
  • 14

1 Answers1

1

Have you seen Everythings seems ok but function retrieveRequestToken() throws 'OAuthCommunicationException' exception?

My guess is your code performs some kind of networking operation in the main thread. If you are targeting the Honeycomb or higher (3.0+), you cannot perform a networking operation on the main thread.

Please see How to fix android.os.NetworkOnMainThreadException? for more info.

Basically, you have to use AsyncTask and move your networking stuffs into doInBackground method. Below's the code. Just to give you the idea.

class NetworkingTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        try {
            String authUrl = provider.retrieveRequestToken(consumer, OAUTH_CALLBACK_URL); 
        ...
        } catch(Exception ex) {

        }
    }
    ...
}
Community
  • 1
  • 1
pt2121
  • 11,720
  • 8
  • 52
  • 69