Generate Server to Server Authentication access token from java
Hello everyone,
I was referring to this URL for generating the access token. I was able to generate access token from Postman software
But was failing from java code. Here is my code snippet.
Created standalone java application, just to get the access token.
Added these maven dependencies.
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-osgi</artifactId>
<version>4.5.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore-osgi</artifactId>
<version>4.4.5</version>
<scope>provided</scope>
</dependency>try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
HttpPost httpPost = new HttpPost("https://ims-na1.adobelogin.com/ims/token/v3");
String postBody = "{\"grant_type\":\"client_credentials\",\"client_id\":\"123456\",\"client_secret\":\"abcd-EFGHIJKL\",\"scope\":\"openid,AdobeID,additional_info.projectedProductContext\"}";
StringEntity jsonInput = new StringEntity(postBody,"UTF-8");
httpPost.setEntity(jsonInput);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
HttpResponse httpResponse = httpclient.execute(httpPost);
// Extract the response's content
HttpEntity responseEntity = httpResponse.getEntity();
String response = EntityUtils.toString(responseEntity);
System.out.println("Response="+response);
System.out.println("status code="+httpResponse.getStatusLine().getStatusCode());
}catch(Exception e) {
System.out.println("error occurred. "+ e.getMessage());
}Note: Added proper client id, secret. Still not getting proper response.