【问题标题】:Using Oltu to connect to Doorkeeper使用 Oltu 连接到 Doorkeeper
【发布时间】:2013-11-01 14:34:55
【问题描述】:

我正在尝试针对作为运行 Doorkeeper 的 OAuth2 提供程序运行的 rails app 进行身份验证。

我正在尝试从Oltu 源修改an example。我目前拥有的代码是:

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());
    }
  }
}

当我使用原始 Facebook 凭据运行此示例时,它会将我带到允许身份验证的页面。当我使用我的 rails 应用程序时,我得到一个表单的 url:

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

当我在浏览器中输入这个时,我被转发到重定向 uri,然后是:

?code=6d09201b18178ee7737fcdd330563143ef0b60855e9d06621dcec627a9c3f29a

当我在提示符处输入代码时,出现以下错误:

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

在针对 Facebook 进行身份验证时,我没有收到该错误。关于我做错了什么有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails oauth-2.0 doorkeeper oltu


    【解决方案1】:

    您可以尝试使用 OAuthJSONAccessTokenResponse 而不是 GitHubTokenResponse

    【讨论】:

    • 这在我包含 org.json.JSONTokener‎ 的 jar 之后工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    • 2014-04-15
    • 2016-06-09
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    相关资源
    最近更新 更多