0

I am developing my application using jTwitter for android. I am able to redirect the user to the authorization page where he enters the credentials to authorize the app. Then the callback function is called. In the callback function am not able to get the access token. It is throwing me exceptions. Kindly help.

Am getting "Communication with the service provider failed" exception. update: am getting the tokens but still getting the exceptions. Here is the code:

   Twitter twitter = null;
private EditText tweetTxt = null;
private Button postBtn = null;

private OAuthSignpostClient client = null;
private String CALLBACK_URI = "myapp://twitt";

private String authUrl = null;
private ProgressDialog postDialog = null;
String[] accessTokenandSecret=null;
String consumerKey="";
String consumerSecret="";
String verifier;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    postBtn=(Button)findViewById(R.id.button1);




    postBtn.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            new MyTask().execute();

        }
    });


    Button postTweet = (Button)findViewById(R.id.button2);
    postTweet.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            new PostTweet().execute();
        }
    });

}


private class PostTweet extends AsyncTask<Void, Void, Void> {


    protected void onPreExecute() {
        Toast.makeText(getApplicationContext(), "pre execute 2", Toast.LENGTH_LONG).show();
        if(accessTokenandSecret==null)
        {
            Toast.makeText(getApplicationContext(), "IT IS NULL", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected Void doInBackground(Void... arg0) {

        client=new OAuthSignpostClient(consumerKey, consumerSecret, accessTokenandSecret[0], accessTokenandSecret[1]);
        Twitter jtwit = new Twitter(null, client);
        jtwit.setStatus("test post from my app");


        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        Toast.makeText(getApplicationContext(), "Tweet is post Successfully!!", Toast.LENGTH_SHORT).show();
    }



}






private class MyTask extends AsyncTask<Void, Void, Void> {


    protected void onPreExecute() {
        Toast.makeText(getApplicationContext(), "pre execute", Toast.LENGTH_LONG).show();
    }

    @Override
    protected Void doInBackground(Void... arg0) {

        client = new OAuthSignpostClient("QvkiReKCHNKcHn3pGrEzQ" ,"EV9vdMRfuT2AQlSNPJW4LhDAyOe0z1mAZYqHJNkPH7g", CALLBACK_URI);

        authUrl = client.authorizeUrl().toString();
        Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getApplicationContext().startActivity(intent);
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        Toast.makeText(getApplicationContext(), "post execute", Toast.LENGTH_LONG).show();
    }



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

protected void onNewIntent(Intent intent) {

    super.onNewIntent(intent);
    Uri uri = intent.getData();
    //Check if you got NewIntent event due to Twitter Call back only

    if (uri != null && uri.toString().startsWith(CALLBACK_URI))
    {
    try
    {
    verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
    Toast.makeText(getApplicationContext(), "inside verfier", Toast.LENGTH_LONG).show();

    client.setAuthorizationCode(verifier);

    accessTokenandSecret=client.getAccessToken();

    //if(accessTokenandSecret==null)
        //{
        //Toast.makeText(getApplicationContext(), "it is null you fool", Toast.LENGTH_LONG).show();
    //  }


    //client=new OAuthSignpostClient(consumerKey, consumerSecret, accessTokenandSecret);
    }
    catch(Exception e){
    Log.d("exception", " "+e.getMessage());
    }
    }}

the log cat errors :

         02-17 15:13:46.599: E/AndroidRuntime(12915): FATAL EXCEPTION: AsyncTask #4
         02-17 15:13:46.599: E/AndroidRuntime(12915): java.lang.RuntimeException: An error occured while executing doInBackground()
         02-17 15:13:46.599: E/AndroidRuntime(12915):   at android.os.AsyncTask$3.done(AsyncTask.java:299)
        02-17 15:13:46.599: E/AndroidRuntime(12915):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
        02-17 15:13:46.599: E/AndroidRuntime(12915):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
        02-17 15:13:46.599: E/AndroidRuntime(12915):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
        02-17 15:13:46.599: E/AndroidRuntime(12915):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
        02-17 15:13:46.599: E/AndroidRuntime(12915):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
        02-17 15:13:46.599: E/AndroidRuntime(12915):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
        02-17 15:13:46.599: E/AndroidRuntime(12915):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        02-17 15:13:46.599: E/AndroidRuntime(12915):    at java.lang.Thread.run(Thread.java:856)
        02-17 15:13:46.599: E/AndroidRuntime(12915): Caused by: winterwell.jtwitter.TwitterException: oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: http://twitter.com/oauth/access_token
        02-17 15:13:46.599: E/AndroidRuntime(12915):    at winterwell.jtwitter.OAuthSignpostClient.setAuthorizationCode(OAuthSignpostClient.java:431)
        02-17 15:13:46.599: E/AndroidRuntime(12915):    at com.example.retrieve.contacts.twitter.MainActivity$GetAccessTok.doInBackground(MainActivity.java:114)
dnivra
  • 749
  • 4
  • 12
  • 30

1 Answers1

1

Could you try downloading the latest version of JTwitter from http://winterwell.com/software/jtwitter.php? There was a bug in AndroidTwitterLogin, where the callback-url wasn't used. That's now been patched.

By the way - be careful about posting your app's oauth details on the web. Once your app is working, I'd advise going to dev.twitter.com and generating fresh details.

Daniel Winterstein
  • 2,418
  • 1
  • 29
  • 41