0

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

我正在尝试修改Oltu来源的示例。我目前拥有的代码是:

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 进行身份验证时,我没有收到该错误。关于我做错了什么的任何想法?

4

1 回答 1

0

您可能会尝试使用 OAuthJSONAccessTokenResponse 而不是 GitHubTokenResponse

于 2013-11-04T08:55:49.827 回答