3

The reference being purely taken from following sites:-

I've developed String Security OAuth2 Facebook integration example, Now I'm looking forward to developed the Security OAuth2 Google (and later Github) integration example where AppID and Secret will be provided to get "access_token" and "refresh_token" etc to be used to access the protected resources like UserDetails etc..

So, first step will be register App on http://code.google.com/apis/console. So it gives me "Client ID" and "Client secret", also I've configured Redirect URI, Done !

Now I've started writing actual Apache OAuth client, but I'm not sure what parameters I need to provide (similarly I provide for Facebook Integration, those parameters were easily available on facebook,while doing google search, but not found for Google), Please provide me suggestions what values should be given for the following blank parameters -

I think I've provided enough information, so any guidance / help / links is appreciated.

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

The following code is developed for callback

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

Added the following code to get protected resources like user profile:

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

2 Answers 2

1

See here for a complete example:

http://mail-archives.apache.org/mod_mbox/oltu-user/201503.mbox/%3CA562FE5D3662044186474F4174F11DAE13044C639F@iowajhnex126.iowa.gov.state.ia.us%3E

4
  • I've modified the code and use the scope (googleapis.com/auth/plus.login googleapis.com/auth/plus.profile.emails.read googleapis.com/auth/plus.me), but I see it's not giving me profile data. Could you please help / guide ?
    – user4618583
    Apr 6, 2015 at 19:26
  • add scope "openId profile email" and in your configured client application enable this scopes as well. If in google console you have not enable google+ related socpes then google will not give you user information. Apr 7, 2015 at 14:04
  • @prtk_shah: I use the .setScope("openId profile email"), still unable to get the profile details. could you please help me out ASAP? Saqib: Thanks. Could you please provide more detailed details. It's not understanding which URL's you've used for.
    – user4618583
    Apr 13, 2015 at 19:15
  • you have to enable "Google+ API" in google developer console. go to console.developers.google.com/project?authuser=0 and click on your project , in your project open "APIs and auth">APIs. search for Google+ API and enable it. Apr 15, 2015 at 11:52
0

I've developed Apache Oltu and Spring integration example and it's working fine at my end.

You need to enable the Google+ API as suggested by @prtk_shah. Thanks.

You need to go to the https://console.developers.google.com/project?authuser=0 and click on your project, in my case it's "apache-oltu", in your open project find option "APIs and auth" --> APIs. search for Google+ API and enable it.

Here you should be able to see this screen. enter image description here

So, I will modify your code below it should be like this:

(IMP) - Your client ID should be like this, For Ex: (755670439314-jcumfghnkmcm72hf40beikvoatknstml.apps.googleusercontent.com), Please make sure it is correct. Fyi - use as it is provided by google developer console

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

The callback code should be:

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

Here is what I'm getting in my example, just wanted to show you

enter image description here

Hope this will be helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.