Solved
Unable to generate Access Token for Oauth Service-to-Service
Hello,
We are using SpringFramework to generate access tokens using OAuth. But, consistently getting "Access Denied". The same credentials work for curl command. Requesting you to please suggest what might be missing
package com.abbott.Oauth.authentication;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import java.util.ArrayList;
import java.util.List;
public class AccessTokenGenerator {
public static void main(String[] args) {
String clientId = "client_id_cred";
String clientSecret = "client_secret_cred";
String tokenUrl = "https://ims-na1.adobelogin.com/ims/token/v2";
// Set up the OAuth2 client details
ClientCredentialsResourceDetails clientDetails = new ClientCredentialsResourceDetails();
clientDetails.setClientId(clientId);
clientDetails.setClientSecret(clientSecret);
clientDetails.setAccessTokenUri(tokenUrl);
List<String> scopes = new ArrayList<String>();
scopes.add("read_pc.dma_aem_ams");
scopes.add("openid");
scopes.add("AdobeID");
scopes.add("read_organizations");
scopes.add("additional_info.projectedProductContext");
clientDetails.setScope(scopes); // Set the desired scope
// Create an OAuth2RestTemplate using the client details
OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(clientDetails);
// Set the request headers
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
// Set the request body parameters
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("grant_type", "client_credentials");
// Make a request to the token endpoint to get the access token
ResponseEntity<OAuth2AccessToken> responseEntity = restTemplate.exchange(
tokenUrl,
HttpMethod.POST,
new org.springframework.http.HttpEntity<>(params, headers),
OAuth2AccessToken.class
);
if (responseEntity.getStatusCode().is2xxSuccessful()) {
OAuth2AccessToken accessToken = responseEntity.getBody();
if (accessToken != null) {
System.out.println("Access Token: " + accessToken.getValue());
System.out.println("Token Type: " + accessToken.getTokenType());
System.out.println("Expires In: " + accessToken.getExpiresIn());
// If a refresh token is provided, you can access it as well
OAuth2RefreshToken refreshToken = accessToken.getRefreshToken();
if (refreshToken != null) {
System.out.println("Refresh Token: " + refreshToken.getValue());
}
}
} else {
System.out.println("Error retrieving access token. Status code: " + responseEntity.getStatusCode());
}
}
}