how to authenticate API xml output | Community
Skip to main content
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Jagadeesh_Prakash

@vineel_k 

Hope Below code will help

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.json.JSONObject;

public class MyOAuthClient {

public String getAccessToken(String clientId, String clientSecret) {
String accessToken = null;

try {
// Create an HttpClient object
HttpClient httpClient = HttpClientBuilder.create().build();

// Create a HttpPost object with the authentication endpoint URL
HttpPost httpPost = new HttpPost("http://localhost:4502/oauth/token");

// Create a JSON object with the grant type, client ID, and client secret
JSONObject json = new JSONObject();
json.put("grant_type", "client_credentials");
json.put("client_id", clientId);
json.put("client_secret", clientSecret);

// Set the JSON object as the entity of the HttpPost request
StringEntity entity = new StringEntity(json.toString());
entity.setContentType("application/json");
httpPost.setEntity(entity);

// Execute the HttpPost request and get the response
HttpResponse response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();

// Parse the response JSON and get the access token
JSONObject responseJson = new JSONObject(responseEntity.getContent());
accessToken = responseJson.getString("access_token");

} catch (Exception e) {
e.printStackTrace();
}

return accessToken;
}

}

 

 

In this code, the getAccessToken method sends an OAuth 2 authentication request to the AEM instance's token endpoint using the client ID and secret provided as arguments. The response is parsed to extract the access token, which is then returned by the method.

2 replies

Jagadeesh_Prakash
New Participant
March 5, 2023

@vineel_k 

Register an OAuth 2 client application in the AEM instance by configuring the "Adobe Granite OAuth Provider" service. This will provide the necessary client ID and secret that will be used to authenticate requests. Obtain an access token by making an OAuth 2 authentication request to the AEM instance. This request will include the client ID and secret, as well as the requested scopes and grant type. The grant type will typically be "client_credentials" for server-to-server authentication.

 

Use the access token to make requests to the AEM XML content API.

vineel_k
vineel_kAuthor
New Participant
March 5, 2023

Hi @jagadeesh_prakash can you share related links for implementation its helps me further dive into it.

Jagadeesh_Prakash
Jagadeesh_PrakashAccepted solution
New Participant
March 5, 2023

@vineel_k 

Hope Below code will help

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.json.JSONObject;

public class MyOAuthClient {

public String getAccessToken(String clientId, String clientSecret) {
String accessToken = null;

try {
// Create an HttpClient object
HttpClient httpClient = HttpClientBuilder.create().build();

// Create a HttpPost object with the authentication endpoint URL
HttpPost httpPost = new HttpPost("http://localhost:4502/oauth/token");

// Create a JSON object with the grant type, client ID, and client secret
JSONObject json = new JSONObject();
json.put("grant_type", "client_credentials");
json.put("client_id", clientId);
json.put("client_secret", clientSecret);

// Set the JSON object as the entity of the HttpPost request
StringEntity entity = new StringEntity(json.toString());
entity.setContentType("application/json");
httpPost.setEntity(entity);

// Execute the HttpPost request and get the response
HttpResponse response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();

// Parse the response JSON and get the access token
JSONObject responseJson = new JSONObject(responseEntity.getContent());
accessToken = responseJson.getString("access_token");

} catch (Exception e) {
e.printStackTrace();
}

return accessToken;
}

}

 

 

In this code, the getAccessToken method sends an OAuth 2 authentication request to the AEM instance's token endpoint using the client ID and secret provided as arguments. The response is parsed to extract the access token, which is then returned by the method.

AMANATH_ULLAH
New Participant
March 5, 2023

@vineel_k 

When you hit the mentioned URL from postman, In the authorization tab select the type as basic Auth and provide AEM username password (such as admin/admin)

 

Amanath Ullah
vineel_k
vineel_kAuthor
New Participant
March 5, 2023

Hi @amanath_ullah i need to authenticate by using Oauth 2 in the code level every time when I hit the URL using postman it need to be secure.