I have 2 pieces of code I want to integrate.
- I have a working Google API (Youtube) integration that gets some user related information using the correct scopes while asking for authentication.
public YouTube getService() throws GeneralSecurityException, IOException {
final NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
return new YouTube.Builder(httpTransport, JSON_FACTORY, getCredentials())
.setApplicationName(APPLICATION_NAME)
.build();
}
private Credential getCredentials() throws IOException {
// With the GoogleAuthorizationCodeFlow you can create a Credential
Credential credential = authorizationCodeFlow.loadCredential(userService.getUserWithAuthorities().get().getId().toString());
if (credential == null) {
GoogleAuthorizationCodeRequestUrl authorizationUrl = authorizationCodeFlow.newAuthorizationUrl();
Map<String, String> state = Map.of("userId", userService.getUserWithAuthorities().get().getId().toString());
authorizationUrl.setState(new ObjectMapper().writeValueAsString(state));
authorizationUrl.setRedirectUri(CALLBACK_URL);
System.out.println("REDIRECTURL: " + authorizationUrl); //ToDo: remove
throw new YtUnauthorizedException(authorizationUrl.toString());
}
return credential;
}
- I have a spring security / keycloack integration with social login using jhipster generated code.
Now I want to use the Google API code with the access token from Spring Security / Keycloak but none of the options I tried worked.
Following this article I tried this code:
GoogleCredential credential = new GoogleCredential().setAccessToken(getAccessToken());
private String getAccessToken(){
Authentication authentication =
SecurityContextHolder
.getContext()
.getAuthentication();
OAuth2AuthenticationToken oauthToken =
(OAuth2AuthenticationToken) authentication;
OAuth2AuthorizedClient client =
clientService.loadAuthorizedClient(
oauthToken.getAuthorizedClientRegistrationId(),
oauthToken.getName());
return client.getAccessToken().getTokenValue();
}
But it doesn't work. I suspect I'm getting the access token of Keycloak but not Google. Any idea how I can use Google API with this setup?