Search code examples
androidauthenticationtwitteroauth-2.0jtwitter

Android Jtwitter, authentication issues


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


Solution

  • 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.