Oauth 2.0 获取OAuthProblemException:使用OLTU时对Google的请求无效

Oauth 2.0 获取OAuthProblemException:使用OLTU时对Google的请求无效,oauth-2.0,google-oauth,oltu,Oauth 2.0,Google Oauth,Oltu,我正在使用Apache中的Oltu库,并尝试使用OAuth2通过Google进行身份验证。以下是相关代码: OAuthAuthzResponse oar = OAuthAuthzResponse.oauthCodeAuthzResponse(request); OAuthClientRequest clientReq = OAuthClientRequest .tokenProvider(OAuthProviderType.GOOGLE) .setClientId("<my

我正在使用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);
OAuthAuthzResponse oar=OAuthAuthzResponse.oauthCodeAuthzResponse(请求); OAuthClientRequest clientReq=OAuthClientRequest .tokenProvider(OAuthProviderType.GOOGLE) .setClientId(“”) .setClientSecret(“”) .setRedirectURI(“https://test.example.com/oauthtest/oauth/google/auth") .setCode(oar.getCode()) .setGrantType(GrantType.AUTHORIZATION_代码) .buildQueryMessage(); OAuthClient OAuthClient=新的OAuthClient(新的URLConnectionClient()); //此调用因OAuthProblemException而失败 OAuthAccessTokenResponse oAuthResponse=oAuthClient.accessToken(clientReq, OAuthJSONAccessTokenResponse.class); 我可以通过Facebook进行身份验证,但无论出于什么原因,这都是失败的。我可以毫无问题地从OAuthAuthzResponse获取代码,因此我知道原始调用正在工作,但这个后续调用失败



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

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


尝试用buildBodyMessage()方法替换buildQueryMessage()。

我有一个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();
此外,在处理回调时,您需要确保发送需要为setCode(“”)设置的“secret”、“TokenLocation”、“RedirectURI”和以上的“code”值

请参考我在中对您的问题的回答。确保您的代码详细信息正确就位