获取OAuthProblemException:使用OLTU时Google的invalid_request

时间:2014-03-11 19:57:12

标签: oauth-2.0 google-oauth oltu

我正在使用Apache的Oltu库,并尝试使用OAuth2通过Google进行身份验证。这是相关代码:

OAuthAuthzResponse oar = OAuthAuthzResponse.oauthCodeAuthzResponse(request);
OAuthClientRequest clientReq = OAuthClientRequest
    .tokenProvider(OAuthProviderType.GOOGLE)
    .setClientId("<my-client-id>")
    .setClientSecret("<my-client-secret>")
    .setRedirectURI("https://test.example.com/oauthtest/oauth/google/auth")
    .setCode(oar.getCode())
    .setGrantType(GrantType.AUTHORIZATION_CODE)
    .buildQueryMessage();

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

// This call fails with the OAuthProblemException
OAuthAccessTokenResponse oAuthResponse = oAuthClient.accessToken(clientReq, 
        OAuthJSONAccessTokenResponse.class);

我可以在没有问题的情况下通过Facebook进行身份验证,但无论出于何种原因,这都是失败的。我可以毫无问题地从OAuthAuthzResponse获取代码,因此我知道原始呼叫正在运行,但此后续呼叫失败。


编辑:我已经放弃使用Oltu并坚持使用HttpClient库并手动执行OAuth舞蹈的简单方法。它对我来说效果更好,我会推荐给任何想要可靠地对多个OAuth提供商进行身份验证的人。只有Twitter要求我使用Scribe

2 个答案:

答案 0 :(得分:4)

Google的OAuth2在HTTP POST方法的主体中发送参数,而不是查询字符串。

尝试通过buildBodyMessage()方法替换buildQueryMessage()。

答案 1 :(得分:1)

我有一个Spring OAuth2和Google集成程序,可以访问受保护的资源,例如用户个人资料等。

我认为你需要重构代码才能使用以下代码:

OAuthClientRequest request = OAuthClientRequest
                .authorizationLocation("https://accounts.google.com/o/oauth2/auth")
                .setClientId("")
                .setRedirectURI("https://test.example.com/oauthtest/oauth/google/auth")
                .setResponseType("code")
                .setScope("https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.profile.emails.read https://www.googleapis.com/auth/plus.me")
                .buildQueryMessage();

此外,当您处理回叫时,您需要确保发送&#34;秘密&#34;,&#34; TokenLocation&#34;,&#34; RedirectURI&#34;以上&#34;代码&#34;您需要为setCode设置的值(&#34;&#34;)

请将 Apache Oltu Spring Security OAuth2 and Google Integration 的回答提交给您的查询。确保您的代码详细信息正确到位。