Bearer/access token using JWT token - .NET or JAVA | Community
Skip to main content
Level 3
Oct 11, 2022 5:26 PM UTC
Solved

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

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/developing/generating-access-tokens-for-server-side-apis.html

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Saravanan_Dharmaraj
Saravanan_DharmarajAccepted solution
New Participant
Oct 11, 2022 6:30 PM UTC

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;
    }

2 replies

Employee
Oct 12, 2022 5:17 AM UTC

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

Translate
Saravanan_Dharmaraj
Saravanan_DharmarajAccepted solution
New Participant
Oct 11, 2022 6:30 PM UTC

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;
    }
Translate