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

What is an Equalizer?

An Equalizer is a java class that extends org.scribe.eq.DefaultEqualizer .

This class has 5 methods you can override:

public String tuneStringToSign(Request request, String toSign, CallType type)
public String tuneOAuthHeader(Request request, String oAuthHeader, CallType type)
public void tuneRequest(Request request, CallType type)
public Token parseRequestTokens(String response)
public Token parseAccessTokens(String response)

They let you add your API-specific code in different parts of the OAuth dance, these are (respectively):

  • Edit the string to sign before the HMAC-SHA1 signature is performed.
  • Edit the OAuth Header (maybe to add a particular attribute, like realm for example)
  • Tune the Request object right before it is sent.
  • Parse the raw string you get from the Request Token step and turn it into a Token object
  • Parse the raw string you get from the Access Token step and turn it into a Token object

Call Type

Each “hook” method receives CallType . This is a java enumeration that specifies which part of the OAuth dance is taking place, so you can act accordingly.

There are 3 types of call:

  • Request Token: When the Consumer asks for a Request Token
  • Access Token: When the Consumer exchanges the Request Token , Request Token Secret and Verifier for an Access Token
  • Resource: Any request for a protected resource made on behalf of the user.

For example, let’s take a look at the (included) LinkedIn Equalizer .

LinkedIn requires the ~ character to be unencoded in the string to sign, and this is only relevant in Resource requests. So we override the tuneStringToSign method and make the relevant changes.

As an exercise to the reader, try to achieve the same with any other OAuth lib :)

Creating your own Equalizer

Suppose you are interacting with a very, very strange OAuth API. It is so weird that It forces you to send the default oauth_timestamp as crazy_oauth_timestamp. That change that usually would drive you nuts it’s easy as pie to implement. Let’s do it!

Create your equalizer class that extends DefaultEqualizer and overrides the desired method.

Check this code

Now link the custom equalizer in the properties file

This is important, in order to tell Scribe to use your custom equalizer you should specify that in the properties file. Like this:

scribe.equalizer=org.crazy.MyCrazyEqualizer

Where org.crazy.MyCrazyEqualizer is the full name of the equalizer class. Not specifying an equalizer will use the default one (that does nothing). Specifying an inexistent will throw a RuntimeError.

I’ve Successfully coded my Equalizer! Hurray!

Cool! now you can contribute it so everyone can use it! Send me a pull request with the equalizer class and the .properties file so I can merge the changes. And thanks again for collaborating! :)