Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Bearer/access token using JWT token - .NET or JAVA

Avatar

Level 4
Level 4

Hi, 

how can we generate a bearer token/access (in Java/ .NET) using following information -

{
"ok": true,
"integration": {
"imsEndpoint": "ims-na1.adobelogin.com",
"metascopes": "111_aem_cloud_api",
"technicalAccount": {
"clientId": "cm-111-11111-integration",
"clientSecret": "1111111"
},
"email": "1111111@techacct.adobe.com",
"id": "1111@techacct.adobe.com",
"org": "1111@AdobeOrg",
"privateKey": "-----BEGIN RSA PRIVATE KEY-----\r\1111111\r\n-----END RSA PRIVATE KEY-----\r\n",
"publicKey": "-----BEGIN CERTIFICATE-----sdfsdfsfd\r\n-----END CERTIFICATE-----\r\n",
"certificateExpirationDate": "2023-09-26T18:22:07.000Z"
},
"statusCode": 200
}
I am looking for code references for Java and .NET applications to generate the bearer/access token using JWT, to invoke AssetAPI available in the AEMaaCS environment.
Note - I am able to generate the token using Node.js application.

Reference -
https://experienceleague.adobe.com/docs/experience-manager-cloud-service/content/implementing/develo...

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

You posted the output of the token call. Hope you have endpoint to pass the credentials and get the token back. Below is the sample code for getting the JWT from the endpoint. 
you need to parse the json to get the token from the JSON response. And the parameters you pass may also vary depends on the system

 

 

public JsonObject getToken() {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        JsonObject jsonObject = null;
        try{

            List<NameValuePair> postParameters = new ArrayList<>();
            postParameters.add(new BasicNameValuePair("client_id", <Client ID>);
            postParameters.add(new BasicNameValuePair("client_secret", <Secret>);
            postParameters.add(new BasicNameValuePair("grant_type", "client_credentials"));
            HttpPost httpPost = new HttpPost(<END Point URL>);
            httpPost.setEntity(new UrlEncodedFormEntity(postParameters, "UTF-8"));

            CloseableHttpResponse response = httpClient.execute(httpPost);
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

            StringBuilder result = new StringBuilder();
            String line ;
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }
            JsonParser jsonParser = new JsonParser();
            jsonObject = jsonParser.parse(result.toString()).getAsJsonObject();

        }catch( IOException e){
            log.error("Exception while invoking getToken  : {} " , e);
        }
        finally{
            try {
                httpClient.close();
            } catch (IOException e) {
                log.error("IOException while invoking getToken Search close : {} " , e);
            }
        }
        return jsonObject;
    }

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

You posted the output of the token call. Hope you have endpoint to pass the credentials and get the token back. Below is the sample code for getting the JWT from the endpoint. 
you need to parse the json to get the token from the JSON response. And the parameters you pass may also vary depends on the system

 

 

public JsonObject getToken() {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        JsonObject jsonObject = null;
        try{

            List<NameValuePair> postParameters = new ArrayList<>();
            postParameters.add(new BasicNameValuePair("client_id", <Client ID>);
            postParameters.add(new BasicNameValuePair("client_secret", <Secret>);
            postParameters.add(new BasicNameValuePair("grant_type", "client_credentials"));
            HttpPost httpPost = new HttpPost(<END Point URL>);
            httpPost.setEntity(new UrlEncodedFormEntity(postParameters, "UTF-8"));

            CloseableHttpResponse response = httpClient.execute(httpPost);
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

            StringBuilder result = new StringBuilder();
            String line ;
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }
            JsonParser jsonParser = new JsonParser();
            jsonObject = jsonParser.parse(result.toString()).getAsJsonObject();

        }catch( IOException e){
            log.error("Exception while invoking getToken  : {} " , e);
        }
        finally{
            try {
                httpClient.close();
            } catch (IOException e) {
                log.error("IOException while invoking getToken Search close : {} " , e);
            }
        }
        return jsonObject;
    }

Avatar

Level 2

Here is a sample IMS Client that might help you -https://github.com/AdobeDocs/analytics-2.0-apis/blob/master/examples/jwt/java/src/main/java/io/adobe/solutions/IMSClient.java