5

Hello I am having difficulty using the JTwitter functions to authenticate with my twitter application. I always get a "TwitterException"

Here is my method

OAuthSignpostClient oauthClient = new OAuthSignpostClient(consumerKey, 
            privateKey, "oob");

a) I don't know what the "oob" value SHOULD be, it is the "callbackURL" and in my application on twitter it says "callBack URL: none" so I have tried putting "none", "None", and null where "oob" with no differing results.

then the rest is boilerplate

 Intent i = new Intent(Intent.ACTION_VIEW, 
    Uri.parse(oauthClient.authorizeUrl().toString()));
    startActivity(i);
    // get the pin
    String v = oauthClient.askUser("Please enter the verification PIN from Twitter");
    oauthClient.setAuthorizationCode(v);
    // Store the authorisation token details for future use
    String[] accessToken = oauthClient.getAccessToken();
    // Next time we can use new OAuthSignpostClient(OAUTH_KEY, OAUTH_SECRET, 
    //        accessToken[0], accessToken[1]) to avoid authenticating again.

    EditText twitterText = (EditText)findViewById(R.id.twitterText);
    twitterText.getText();

    // Make a Twitter object
    Twitter twitter = new Twitter(null, oauthClient);
    // Print Daniel Winterstein's status
    //System.out.println(twitter.getStatus("winterstein"));
    // Set my status
    twitter.setStatus(twitterText.getText());

at this point, I'm simply not sure on how to make this work. Wish I could be more verbose about it, but it has something to do with the authentication. Online things I've seen haven't been helpful

j0k
  • 22,600
  • 28
  • 79
  • 90
CQM
  • 42,592
  • 75
  • 224
  • 366

3 Answers3

7

Try this as your callback url:

OAuthSignpostClient oauthClient = 
    new OAuthSignpostClient(consumerKey, privateKey, "callback://twitter");  

Remember! After:

    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));

Do the following

override onResume() to get the verification code

protected void onResume() {
    super.onResume();
    if (this.getIntent()!=null && this.getIntent().getData()!=null){
        Uri uri = this.getIntent().getData();
        if (uri != null && uri.toString().startsWith("callback://twitter")) {
            //Do whatever you want
            //usually use uri.getQueryParameter(someString);
            //test what could be someString using Log.v("",uri.toString());
            //you want the Authorization code which is a couple of numbers
            //so you could use oauthClient.setAuthorizationCode(v); 
            //and finally initialise Twitter
        }
    }
}

You can use uri.getQueryParameterNames() to get the parameter String names.


@BobVork

I'd just like to add that you'll need to have a callback URL set in the Settings tab for your Twitter app (on twitter.com).

It doesn't even matter what you put in the field, but if it's empty, callback urls will not work.

halfer
  • 19,824
  • 17
  • 99
  • 186
Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
  • 1
    I'd just like to add that you'll need to have a callback URL set in the Settings tab for your Twitter app (on twitter.com). It doesn't even matter what you put in the field, but if it's empty, callback urls will not work. This took me about an hour to find out, so I thought this might save somebody else some time. – Bob Vork Oct 19 '11 at 12:17
1

I'm not sure that's the problem, but it seems like you never set the user name for the twitter object.

MByD
  • 135,866
  • 28
  • 264
  • 277
  • I thought the point of oath was so that the user doesn't have to put their username in or something... much less hard code it? The concept kind of confuses, my first intuition was that username goes somewhere, but then the twitter object seemed to not require it and someone oath would take care of it – CQM Aug 05 '11 at 21:31
  • I am not that familiar with twitter APIs, but everywhere I looked (even in the example you took the code from), even when oAuth was used, the user name was used too. You can keep it once and then use it when needed, you don't have to read it from some TextView each time. – MByD Aug 05 '11 at 21:33
1

Well, I don't know how it is with the Jtwitter API and I don't know enough Java at this point to really understand the inner workings of this code, but have you tried, instead of NULL or None, to try ? instead. That is the callback used with the restful API when making calls from Javascript. Try it out.

Chamilyan
  • 9,347
  • 10
  • 38
  • 67