Getting OAuthProblemException: invalid_request for Google when using OLTU

I am using the Oltu library from Apache and I am trying to authenticate via Google using OAuth2. Here is the relevant code:

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); 

I can authenticate via Facebook without problems, but for some reason this fails. I can get the code from OAuthAuthzResponse without a problem, so I know that the original call works, but this subsequent call does not work.


Edit: I gave up using Oltu and took a simpler approach to using the HttpClient library and doing the OAuth dance manually. This worked better for me, and I would recommend it to anyone who reliably authenticates more than one OAuth provider. Only Twitter required me to use Scribe .

+7
google-oauth oltu
source share
2 answers

Google OAuth2 sends parameters to the body of the HTTP POST method instead of the query string.

Try replacing the buildQueryMessage () method with the buildBodyMessage () method.

+4
source share

I have a program for integration with OAuth2 and Google Spring, where I can access protected resources such as user profile, etc.

I think you need to reorganize the code to use the following code:

 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(); 

Also, when you handle the callback, you need to make sure that you send the value "secret", "TokenLocation", "RedirectURI" and above the "code", which must be set for setCode ("")

Please write my answer from Apache Oltu Spring OAuth2 Security and Google Integration . Make sure your code details are set correctly.

+1
source share

All Articles