Skip to content
fernandezpablo85 edited this page Sep 14, 2010 · 13 revisions

Install dependencies

Scribe has only one dependency and that is Apache commons codec so go grab that.

Build it

Download the code and then build it using maven to get the latest release, or you can download the jars directly from the Downloads page

Use it

Create Scribe

Once you have the jar on your classpath, you should create the Scribe object like this:

Scribe scribe = new Scribe(props);

Where props is a java.util.Properties instance with the configuration data for your requests.Scribe distribution comes with properties for the most common APIs, let’s see the twitter properties file:

consumer.key=your consumer key
consumer.secret=your consumer secret 
request.token.verb=POST
request.token.url=http://twitter.com/oauth/request_token
access.token.verb=POST
access.token.url=http://twitter.com/oauth/access_token
callback.url=http://localhost:8081/authz

This contains your key pair, the verbs and url of your API endpoints and your callback url.

Note: This file also contains a scribe.equalizer property that you might want to use if you are dealing with a non-standard API. If so please check the Equalizers page.

Get the Request Token

The first step in the OAuth flow is to get the Request Token. For this just do:

Token requestToken = scribe.getRequestToken();

This should return a Token object, that contains the oauth_token and oauth_secret.

Authorize the Request Token

Now that you have the Request Token you need to redirect your user to the authorization page, where he will (surprise!) authorize your application.

Note: for OOB please check Out Of Band

The library does not help a lot here, and you probably need to create an HTTP Request to the API specific authorization url, appending the Request Token you obtained in the previous step. Once you do that, you’ll get the verifier.

Change the Request Token for the Access Token

Once you have the Request Token_, Request Token Secret and the Verifier you can change those for the Access Tokens like this:

Token accessToken = scribe.getAccessToken(requestToken, "Verifier");

The accessToken variable now contains both Access Token and Access Token Secret. Hell yeah! now with these two you can sign the requests against your API. Congratulations!

Making the Request

Now that the OAuth dance (as a former teammate likes to call it) has ended, you can make the actual call. For this just do the following:

Request request = new Request(Verb.GET, "http://api.twitter.com/1/statuses/retweets/id.xml");

scribe.signRequest(request, accessToken);

Response response = request.send();

The Response object contains everything you want now!

Note: Both Request and Response are Scribe objects. If you want to use standard java Requests, check the Request Wrappers page.

Done!

That’s it! Happy hacking!

Clone this wiki locally