views:

82

answers:

1

I have an application that runs on a server, listens for various tweets using the streaming api, and then occasionally sends tweets out. This worked fine using the old twitter authentication system, but using OAuth has complicated things a bit.

I can successfully start the application up and my browser opens Twitter's OAuth page. If I enter the PIN the application runs fine but I need to be able to restart the application unattended. Twitter's OAuth FAQ http://dev.twitter.com/pages/oauth_faq says that tokens never expire, so what I'd like to do is re-use the authorization token each time the application starts.

How can I do that? Or is there another way to authorize just once?

A: 

I'll answer my own question because the solution is pretty straightforward.

As described in this page http://dev.twitter.com/pages/oauth_single_token if you're building an application with single-user use cases you can retrieve the permanent access token and access token secrets from your application control panel on Twitter. In the Application Details page, click the My Access Token link.

Reading through the OAuthSignPostClient class I noticed that the default jtwitter implementation uses the following method signature:

    public OAuthSignpostClient(String consumerKey, String consumerSecret, 
String callbackUrl)

But it can also be called using:

public OAuthSignpostClient(String consumerKey, String consumerSecret, 
            String accessToken, String accessTokenSecret) 

So my client app code has changed from:

    OAuthSignpostClient client = new OAuthSignpostClient(MY_OAUTH_KEY, 
MY_OAUTH_SECRET, "oob");

to:

    OAuthSignpostClient client = new OAuthSignpostClient(MY_OAUTH_KEY, 
MY_OAUTH_SECRET, MY_ACCESS_TOKEN, MY_ACCESS_TOKEN_SECRET);

And all is well.

Hibiscus