【问题标题】:Apache Oltu Spring Security OAuth2 and Google IntegrationApache Oltu Spring Security OAuth2 和 Google 集成
【发布时间】:2015-06-10 06:00:13
【问题描述】:

参考资料纯粹取自以下网站:-

我已经开发了 String Security OAuth2 Facebook 集成示例,现在我期待开发 Security OAuth2 Google(以及后来的 Github)集成示例,其中将提供 AppID 和 Secret 以获得“ access_token" 和 "refresh_token" 等用于访问受保护的资源,如 UserDetails 等。

所以,第一步是在http://code.google.com/apis/console 上注册应用程序。所以它给了我“客户端 ID”和“客户端密码”,我还配置了重定向 URI,完成!

现在我已经开始编写实际的 Apache OAuth 客户端,但我不确定我需要提供哪些参数(类似于我为 Facebook 集成提供的参数,这些参数在 facebook 上很容易获得,同时进行谷歌搜索,但没有找到对于 Google),请给我建议应该为以下空白参数提供哪些值 -

我想我已经提供了足够的信息,所以任何指导/帮助/链接都非常感谢。

OAuthClientRequest request = OAuthClientRequest
                .authorizationLocation("")
                .setClientId("3kT21Hlkzzt5eV1")
                .setRedirectURI("http://localhost:8080/apache-oltu/google/redirect")
                .setResponseType("")
                .setScope("")
                .buildQueryMessage();

以下代码是为回调开发的

private void getAccessToken(String authorizationCode) throws OAuthSystemException, OAuthProblemException {
        OAuthClientRequest request = OAuthClientRequest
                .tokenLocation("")
                .setGrantType()
                .setClientId("3kT21H5EO3zzt5eV1")
                .setClientSecret("1kT21Hdlkzzt5eV1")
                .setRedirectURI("http://localhost:8080/apache-oltu/google/redirect")
                .setCode()
                .buildBodyMessage();

添加以下代码以获取受保护的资源,例如用户配置文件:

request= new OAuthBearerClientRequest("https://www.googleapis.com/auth/userinfo.profile").
                    setAccessToken(oAuthResponse.getAccessToken()).
                    buildQueryMessage();

【问题讨论】:

    标签: spring-security oauth-2.0 google-oauth oltu


    【解决方案1】:

    【讨论】:

    • 我已经修改了代码并使用了范围 (googleapis.com/auth/plus.login googleapis.com/auth/plus.profile.emails.read googleapis.com/auth/plus.me),但我发现它没有给我个人资料数据。你能帮忙/指导吗?
    • 添加范围“openId profile email”并在您配置的客户端应用程序中启用此范围。如果在 google 控制台中你没有启用 google+ 相关的 socpes,那么 google 不会给你用户信息。
    • @prtk_shah:我使用 .setScope("openId profile email"),仍然无法获取配置文件详细信息。你能尽快帮助我吗?萨奇布:谢谢。您能否提供更详细的细节。它不知道您使用的是哪个 URL。
    • 您必须在谷歌开发者控制台中启用“Google+ API”。转到console.developers.google.com/project?authuser=0 并单击您的项目,在您的项目中打开“APIs and auth”>APIs。搜索 Google+ API 并启用它。
    【解决方案2】:

    我开发了 Apache Oltu 和 Spring 集成示例,它在我的最后运行良好。

    您需要按照@prtk_shah 的建议启用 Google+ API。谢谢。

    您需要转到https://console.developers.google.com/project?authuser=0 并单击您的项目,在我的情况下它是“apache-oltu”,在您打开的项目中找到选项“APIs and auth”-> APIs。搜索 Google+ API 并启用它。

    在这里您应该可以看到这个屏幕。

    所以,我会修改你下面的代码应该是这样的:

    (IMP) - 您的客户端 ID 应该是这样的,例如:(755670439314-jcumfghnkmcm72hf40beikvoatknstml.apps.googleusercontent.com),请确保它是正确的。仅供参考 - 由谷歌开发者控制台提供使用

    OAuthClientRequest request = OAuthClientRequest
                    .authorizationLocation("https://accounts.google.com/o/oauth2/auth")
                    .setClientId("3kT21Hlkzzt5eV1.apps.googleusercontent.com")
                    .setRedirectURI("Give your projects redirect URI")
                    .setResponseType("responsecode")
                    .setScope("openId profile email")
                    .buildQueryMessage();
    

    回调代码应该是:

    private void getAccessToken(String authorizationCode) throws OAuthSystemException, OAuthProblemException {
            OAuthClientRequest request = OAuthClientRequest
                    .tokenLocation("https://accounts.google.com/o/oauth2/token")
                    .setGrantType(GrantType.AUTHORIZATION_CODE)
                    .setClientId("give your complete client id")
                    .setClientSecret("give your secret")
                    .setRedirectURI("This will be your callback or Redirect URL (Give it correctly)")
                    .setCode(authorizationCode)
                    .buildBodyMessage();
    

    这是我在示例中得到的内容,只是想向您展示

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2015-07-12
      • 1970-01-01
      • 2015-11-11
      • 1970-01-01
      • 2017-02-19
      • 2017-08-01
      • 2015-02-26
      • 2017-05-03
      • 2019-09-25
      相关资源
      最近更新 更多