Skip to content

Instantly share code, notes, and snippets.

@mgp
Created December 2, 2015 18:04
Show Gist options
  • Save mgp/5968ceb0a83294326793 to your computer and use it in GitHub Desktop.
Save mgp/5968ceb0a83294326793 to your computer and use it in GitHub Desktop.
signpost-okhttp-blog

5. Consider Signpost for OAuth 1.0a

We initially turned to Scribe for our OAuth 1.0a needs, but it has its own Request class that uses java.net.HttpURLConnection to send and receive data. Therefore we could not use Stetho to inspect the data. Later we migrated to Signpost, which is a lightweight OAuth 1.0a library that allows using whatever networking layer you already have. All you must do is adapt instances of your request and response classes to the HttpRequest and HttpResponse interfaces defined by Signpost.

Our OkHttpOAuthRequest class adapts a com.squareup.okhttp.Request instance to the HttpRequest interface of Signpost. The implementation is quite straightforward. You can read OkHttpOAuthRequest.java in its entirety here, but here is an excerpt:

@Override
public String getHeader(String name) {
  return mRequest.header(name);
}

@Override
public String getContentType() {
  RequestBody requestBody = mRequest.body();
  if (requestBody != null) {
    MediaType contentType = mRequest.body().contentType();
    if (contentType != null) {
      return contentType.toString();
    }
  }
  return null;
}

Similarly, our OkHttpOAuthResponse class adapts a com.squareup.okhttp.Response instance to the HttpResponse interface of Signpost. You can read OkHttpOAuthResponse.java in its entirety here.

If you wish to implement a consumer, extend the oauth.signpost.AbstractOAuthConsumer class and override its wrap method to TODO:

public class OkHttpOAuthConsumer extends AbstractOAuthConsumer {
  public OkHttpOAuthConsumer(OAuthConsumerValues oauthConsumerValues) {
    super(oauthConsumerValues.key(), oauthConsumerValues.secret());
  }

  @Override
  protected OkHttpOAuthRequest wrap(Object request) {
    return new OkHttpOAuthRequest((Request) request);
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment