ruby-on-railsoauth-2.0doorkeeperoltu

Using Oltu to connect to Doorkeeper


I'm attempting to authenticate against a rails app running as an OAuth2 provider running Doorkeeper.

I'm attempting to modify an example from the Oltu sources. The code that I have currently is:

public class OAuthClientTest {
  public static void main(String[] args) throws OAuthSystemException, IOException {
    String authUri = "http://smoke-track.herokuapp.com/oauth/authorize";
    String callback = "http://localhost:8080";
    String clientId = "728ad798943fff1afd90e79765e9534ef52a5b166cfd25f055d1c8ff6f3ae7fd";
    String secret = "3728e0449052b616e2465c04d3cbd792f2d37e70ca64075708bfe8b53c28d529";
    String tokenUri = "http://smoke-track.herokuapp.com/oauth/token";

    try {
      OAuthClientRequest request = OAuthClientRequest
         .authorizationLocation(authUri)
         .setClientId(clientId)
         .setRedirectURI(callback)
         .setResponseType("code")
         .buildQueryMessage();

      System.out.println("Visit: " + request.getLocationUri() + "\nand grant permission");

      System.out.print("Now enter the OAuth code you have received in redirect uri ");
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      String code = br.readLine();

      request = OAuthClientRequest
         .tokenLocation(tokenUri)
         .setGrantType(GrantType.AUTHORIZATION_CODE)
         .setClientId(clientId)
         .setClientSecret(secret)
         .setRedirectURI(callback)
         .setCode(code)
         .buildBodyMessage();

      OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());

      //Facebook is not fully compatible with OAuth 2.0 draft 10, access token response is
      //application/x-www-form-urlencoded, not json encoded so we use dedicated response class for that
      //Own response class is an easy way to deal with oauth providers that introduce modifications to
      //OAuth specification
      GitHubTokenResponse oAuthResponse = oAuthClient.accessToken(request, GitHubTokenResponse.class);

      System.out.println(
         "Access Token: " + oAuthResponse.getAccessToken() + ", Expires in: " + oAuthResponse
             .getExpiresIn());
    } catch (OAuthProblemException e) {
      System.out.println("OAuth error: " + e.getError());
      System.out.println("OAuth error description: " + e.getDescription());
    }
  }
}

When I run this example with the original Facebook credentials, it takes me to a page that allows authentication. When I use my rails app, I get a url of the form:

http://smoke-track.herokuapp.com/oauth/authorize?response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A8080&client_id=728ad798943fff1afd90e79765e9534ef52a5b166cfd25f055d1c8ff6f3ae7fd

When I enter this in the browser I am forwarded to the redirect uri followed by:

?code=6d09201b18178ee7737fcdd330563143ef0b60855e9d06621dcec627a9c3f29a

When I enter the code at the prompt, I get the following errors:

OAuth error: invalid_request
OAuth error description: Missing parameters: access_token

I don't get that error when authenticating against Facebook. Any ideas as to what I am doing wrong?


Solution

  • you might try to use OAuthJSONAccessTokenResponse rather than GitHubTokenResponse