首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Oltu连接到门卫

使用Oltu连接到门卫
EN

Stack Overflow用户
提问于 2013-11-01 14:34:55
回答 1查看 381关注 0票数 0

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

我试图从一个例子源代码中修改Oltu。我目前的代码是:

代码语言:javascript
复制
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:

代码语言:javascript
复制
http://smoke-track.herokuapp.com/oauth/authorize?response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A8080&client_id=728ad798943fff1afd90e79765e9534ef52a5b166cfd25f055d1c8ff6f3ae7fd

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

代码语言:javascript
复制
?code=6d09201b18178ee7737fcdd330563143ef0b60855e9d06621dcec627a9c3f29a

当我在提示符下输入代码时,会得到以下错误:

代码语言:javascript
复制
OAuth error: invalid_request
OAuth error description: Missing parameters: access_token

在对Facebook进行身份验证时,我不明白这个错误。对我做错了什么有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-04 08:55:49

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19728882

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档