Hard Coded User Authorization

Hard coded user authorization is the process where you already have permission from the user in the form of an access token and access token secret.  There are two ways you can get those items.  The normal way is to have saved them from a previous web- or pin-based authorization ((the samples for those two types of authorization show how to retrieve the access token and access token secret)).  

The other way is mostly useful for debugging; if you to into the details section for your application, you can see a section called “Your access token” ((if you don’t see a set of tokens listed, you need to click the “Create my access token” link first)):

Click to enlarge

Click to enlarge

The tokens you see there are the same ones you’d get from authorizing yourself through more traditional means.

Regardless of whether you’ve saved the tokens from a previous call or whether you’ve extracted them from the Twitter developer’s web site, the code for doing the authorization is the same.

// Get the root twitter object
Twitter twitter = TwitterFactory.getSingleton();

// Set up the access tokens and keys to get permission to access
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
AccessToken at=new AccessToken(ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
twitter.setOAuthAccessToken(at);

This is simpler than Application Authorization, but only because you’ve hard coded the results of some other Twitter authorization interaction.

You can download the following example program that demonstrates Twitter Authorization with hard coded keys and secrets: HardCodedAuthentication.java

Leave a Reply