Skip to content
fernandezpablo85 edited this page Jun 3, 2012 · 16 revisions

Getting Started With Scribe (AKA ‘OAuth made so easy your grandma can do it’)

In this tutorial we will walk through an example on how to use Scribe with Twitter.

For an executable example please go here

Step Zero: Install Scribe

First of all, you need to install scribe. You either download the jar manually from the downloads page and include apache commons codec, or just let maven take care of everything adding this to your pom.xml file:

<dependency>
  <groupId>org.scribe</groupId>
  <artifactId>scribe</artifactId>
  <version>1.3.0</version> // please use always the latest version
</dependency>

Step One: Create the OAuthService object

OAuthService service = new ServiceBuilder()
                           .provider(TwitterApi.class)
                           .apiKey("your_api_key")
                           .apiSecret("your_api_secret")
                           .build();

Yup, that’s it. :)

The example uses OOB OAuth, if you want to pass a callbackUrl so that Twitter redirects you there just do:

OAuthService service = new ServiceBuilder()
                           .provider(TwitterApi.class)
                           .apiKey("your_api_key")
                           .apiSecret("your_api_secret")
                           .callback("http://your.site.com/")
                           .build();

Step Two: Get the request token

Token requestToken = service.getRequestToken()

Easy right?

Step Three: Making the user validate your request token

Let’s help your users authorize you app to do the OAuth calls.

For this you need to redirect them to the following URL:

String authUrl = service.getAuthorizationUrl(requestToken)

After this either the user will get a verifier code (if this is an OOB request) or you’ll receive a redirect from Twitter with the verifier and the requestToken on it (if you provided a callbackUrl)

Step Four: Get the access Token

Now that you have (somehow) the verifier, you need to exchange your requestToken and verifier for an accessToken which is the one used to sign requests.

Verifier verifier = new Verifier("verifier you got from the user");
Token accessToken = service.getAccessToken(requestToken, verifier); // the requestToken you had from step 2

Step Five: Sign request

You are all set to make your first API call, so let’s do it!

OAuthRequest request = new OAuthRequest(Verb.GET, "http://api.twitter.com/1/account/verify_credentials.xml");
service.signRequest(accessToken, request); // the access token from step 4
Response response = request.send();
System.out.println(response.getBody());

That’s it! You already know everything you need to start building a cool OAuth application.

Good Luck!

Doubts – Comments

It’s really useful to use the debug function of scribe for troubleshoot. See:

OAuthService service = new ServiceBuilder()
                           .provider(TwitterApi.class)
                           .apiKey("your_api_key")
                           .apiSecret("your_api_secret")
                           .callback("http://your.site.com/")
                           .debug()
                           .build();

That will print useful debug information to `System.out`, there’s also an overloaded version that takes an `OutputStream`.

email me at:

fernandezpablo85@gmail.com

or ping me via Twitter at:

@fernandezpablo

Clone this wiki locally