All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.cloud.rest.api.client.auth.oauth.ClientCredentialsAccessTokenProvider Maven / Gradle / Ivy

Go to download

Java HTTP client library for HTTP handling, when building clients for RESTful APIs.

The newest version!
package com.sap.cloud.rest.api.client.auth.oauth;

import com.sap.cloud.rest.api.client.RequestBuilder;
import com.sap.cloud.rest.api.client.RestApiClient;
import com.sap.cloud.rest.api.client.config.RestApiClientConfig;
import com.sap.cloud.rest.api.client.http.HttpClientProvider;
import com.sap.cloud.rest.api.client.model.Request;
import com.sap.cloud.rest.api.client.utils.JacksonJsonResponseHandler;

public class ClientCredentialsAccessTokenProvider extends RestApiClient implements AccessTokenProvider {

    private static final String RESPONSE_TYPE = "response_type";
    private static final String TOKEN = "token";

    private static final String GRANT_TYPE = "grant_type";
    private static final String CLIENT_CREDENTIALS = "client_credentials";

    private String apiPath;

    public ClientCredentialsAccessTokenProvider(RestApiClientConfig restApiClientConfig, String apiPath) {
        super(restApiClientConfig);

        this.apiPath = apiPath;
    }

    public ClientCredentialsAccessTokenProvider(RestApiClientConfig restApiClientConfig,
            HttpClientProvider httpClientProvider, String apiPath) {
        super(restApiClientConfig, httpClientProvider);

        this.apiPath = apiPath;
    }

    @Override
    public String retrieveAccessToken() {
        Request retrieveAccessTokenRequest = getAccessTokenRequestBuilder().build();

        AccessTokenResponseDto accessTokenResponse = execute(retrieveAccessTokenRequest,
                new JacksonJsonResponseHandler<>(AccessTokenResponseDto.class)).getEntity();
        return accessTokenResponse.getAccessToken();
    }

    public RequestBuilder getAccessTokenRequestBuilder() {
        return RequestBuilder
                .postRequest()
                .uri(buildRequestUri())
                .addParameter(GRANT_TYPE, CLIENT_CREDENTIALS)
                .addParameter(RESPONSE_TYPE, TOKEN);
    }

    @Override
    protected String getApiPath() {
        return apiPath;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy