Expand my Community achievements bar.

author 401 error

Avatar

Level 4

Below is my code to servlet

package com.***.core.servlets;

@Component(service = { Servlet.class })
@SlingServletResourceTypes(resourceTypes = "dcp/servlet/assets-locked", methods = HttpConstants.METHOD_GET, extensions = "json")
public class DCPLockedAssetsRequestServlet extends SlingSafeMethodsServlet {
       @Override
   protected void doGet(SlingHttpServletRequest requestSlingHttpServletResponse responsethrows IOException {
        response.setContentType("application/json");
        LOG.info("Forwarding POST request to author instance with Basic Authentication.");
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // Adapt session and validate user
        Session session = request.getResourceResolver().adaptTo(Session.class);
        String userId = session.getUserID();   
        UserManager userManager = request.getResourceResolver().adaptTo(UserManager.class);
        Map<StringStringpayloadMap = new HashMap<StringString>();
            currentUser = userManager.getAuthorizable(userId);            
            payloadMap.put("assetsPath", assetPath);
            payloadMap.put("userId", userId);
            payloadMap.put("userPath"currentUser.getPath().toString());
            payloadMap.put("principalName"currentUser.getPrincipal().getName());             
        String authorServiceURL = techCredentialService.getAuthorServletURL();
        // Prepare HTTP POST request to author
        HttpPost postRequest = new HttpPost(authorServiceURL);
            token = dcpUpdateUserService.getToken();
            postRequest.setHeader("Authorization", "Bearer " + token);
        postRequest.setHeader(***Constants.CONTENT_TYPE, ***Constants.CONTENT_TYPE_JSON);        
        postRequest.setEntity(new StringEntity(new Gson().toJson(payloadMap)));
       
        // Execute the HTTP request
        org.apache.http.HttpResponse authorResponse = httpClient.execute(postRequest);       
        if (statusCode == 200) {
            String responseContent = EntityUtils.toString(authorResponse.getEntity());
            JsonObject jsonResponse = new JsonObject();
            jsonResponse.addProperty(***Constants.STATUS, "success");
            jsonResponse.addProperty(***Constants.MESSAGE, "Workflow triggered successfully.");
            response.getWriter().write(jsonResponse.toString());
         }}

i am hitting this servlet from publisher,
through this servlet when i hit the author path i.e 
https://author-***-***.adobeaemcloud.com/content/api/***/2025.4/locked-assets-author.json

it gives us 401 unauthorised from author

we have used the tech credentials of the techaccount user, through that we are getting access token and providing to it and we have given all the permission to that useron root jcr:All, but its giving 401 then also

Please, ASAP can i get the help??
2 Replies

Avatar

Community Advisor

Hi,

 

Clearly, the issue lies with the token. Please debug and validate that the Authorization header is correctly formed, and confirm that the token you're using is valid. You can try hitting the author instance using the token via Postman or another tool to ensure it’s working properly.

 

Please check this example: https://experienceleague.adobe.com/en/docs/experience-manager-learn/getting-started-with-aem-headles...

 

Hope this helps!



Esteban Bustamante

Avatar

Community Advisor

Hi @bhavigoyal , 

401 unauthorised error suggest token is expired or not valid(malformed/null).

Either debug or just log the value of token after statement token = dcpUpdateUserService.getToken();

Just make sure in general token have expiration time so hopefully service would be either generating token each time or would be regenerating before token get expired.

Thanks