Search in sources :

Example 1 with ClientCredentialsTokenRequest

use of com.google.api.client.auth.oauth2.ClientCredentialsTokenRequest in project SORMAS-Project by hzi-braunschweig.

the class Oidc method requestAccessToken.

public static String requestAccessToken(String tokenEndpoint, String clientId, String clientSecret, List<String> scopes) throws Exception {
    ClientParametersAuthentication clientAuth = new ClientParametersAuthentication(clientId, clientSecret);
    try {
        LOGGER.info(String.format("Requesting access token for client %s at %s with scope: %s", clientId, tokenEndpoint, scopes));
        TokenResponse response = new ClientCredentialsTokenRequest(new NetHttpTransport(), new GsonFactory(), new GenericUrl(tokenEndpoint)).setClientAuthentication(clientAuth).setScopes(scopes).execute();
        String token = response.getAccessToken();
        if (token == null || token.isEmpty()) {
            LOGGER.error("Could not retrieve access token.");
            throw new Exception("Could not retrieve access token.");
        }
        return token;
    } catch (IOException e) {
        LOGGER.error("Unable to connect to Keycloak.", e);
        throw e;
    }
}
Also used : ClientCredentialsTokenRequest(com.google.api.client.auth.oauth2.ClientCredentialsTokenRequest) ClientParametersAuthentication(com.google.api.client.auth.oauth2.ClientParametersAuthentication) GsonFactory(com.google.api.client.json.gson.GsonFactory) TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

ClientCredentialsTokenRequest (com.google.api.client.auth.oauth2.ClientCredentialsTokenRequest)1 ClientParametersAuthentication (com.google.api.client.auth.oauth2.ClientParametersAuthentication)1 TokenResponse (com.google.api.client.auth.oauth2.TokenResponse)1 GenericUrl (com.google.api.client.http.GenericUrl)1 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)1 GsonFactory (com.google.api.client.json.gson.GsonFactory)1 IOException (java.io.IOException)1