1
votes

I want to post a simple status message to a Twitter account that's linked to my app. All users of my app will post to the same Twitter account.

I've registered my app with Twitter (according to the guidance given here: How to post a tweet from an Android app to one specific account?) and I have the necessary ConsumerKey, ConsumerSecret, AccessToken and AccessTokenSecret. I've set the account to Read & Write, and set the REQUEST type to GET.

I'm using Twitter4J and installed the twitter4j-core-3.0.3.jar into my app. The Manifest file has the required "android.permission.INTERNET". This is the code …::

AccessToken a = new AccessToken(AccessToken, AccessTokenSecret);
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(ConsumerKey, ConsumerSecret);
twitter.setOAuthAccessToken(a);

try
{
    twitter.updateStatus("Tweet Test #1");
    Log.v(TAG, "Twitter Tweet sent!");
}
catch (TwitterException e)
{
    // TODO Auto-generated catch block
    Log.e(TAG, "Error sending Tweet:" + e.getMessage());
}

The twitter.updateStatus("xxxxxx") call causes an exception that reports “Received authentication challenge is null” in the logcat.

I assumed I could just post, but it seems Twitter wants something more?

Can anybody offer any advice as to what I'm doing wrong?

2
Have you verified that the time is set correctly on the client device as recommended here, here and here?George Cummins
@George:OK, that's useful info. I'm sure that the time/date was correct last time I tried it, but I'll check again. Does it have to be USA time, or are the servers based in the UK? And the CallbackURL is something else to look at. I didn't think I needed one as I'm not trying to Authorise in my app on-the-fly, I only want to send updates to a pre-defined and fixed Twitter account. I thought the oAuth/CallbackURL bit was bypassed by getting the 4 keys/tokens directly from Twitter, but if Twitter is fussy about having one, even if it's an unused dummy URL, I'll give it a go. Thanks!DDSports
twitter api uses GMT timezone, but just set the system time of your android phone or emulator correctly...mb21

2 Answers

2
votes

Managed to get to the bottom of this in the end!

My particular problem was that I didn't enter a Callback URL in the Twitter Apps setup page. I'm only interested in sending tweets to my app's linked Twitter account and I already have the 4 tokens/secrets, so I don't need to get the user to authorise thier own account via my app. As such, I don't need a Callback URL.

Unfortunately, the Twitter apps page lets you leave that field blank when you request the tokens/secret, and they don't make it clear that the Callback URL is a required field. If you don't need it, you can put absolutely anything you like in there; But if you leave it blank, Twitter won't let you tweet from your app! Setting the access type to "Read & Write" is good enough just to update status, but set it to "Read, Write & Direct Messages" if you want to do more.

Some tutorials say you should set the app type to "Browser" (instead of "Desktop"), but that option seems to have disappeared from the Twitter apps page, so I guess that's no longer important.

I managed to find some very good tutorials about tweeting from an Android app (here, here and, in particular, here) which make it clear that the Callback URL is a requirement, and go on to explain very clearly how to get it to work.

0
votes

Have you checked to make sure there aren't any extra whitespaces in your Twitter4J configuration file? Double-check each of your consumer.. and access.. fields just in case.