Expand my Community achievements bar.

SOLVED

how to authenticate API xml output

Avatar

Level 2

I got a requirement

when i hit http://localhost:4502/content/website/api/jcr:content.api.xml URL through the postman i need to authenticate it.

 

Any Suggestions how to authenticate.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@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.

View solution in original post

5 Replies

Avatar

Level 7

@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)

 

Avatar

Level 2

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.

Avatar

Community Advisor

@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.

Avatar

Level 2

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

Avatar

Correct answer by
Community Advisor

@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.