Calling AEM REST API always results in 401 Unauthorized | Community
Skip to main content
Level 2
March 9, 2024
Question

Calling AEM REST API always results in 401 Unauthorized

  • March 9, 2024
  • 1 reply
  • 1049 views

Hello,

I created a c# application that is attempting to use the AEM API to upload assets into our AEM As a Cloud Service instance.  I am able to use the API to generate a valid token, but whenever I try to call any AEM APIs with that token, I always get a 401 - Unauthorized response.  If I use the same url and token in Postman, it works everytime.  Here is my code for reference:

using (var lClient = new HttpClient()) { // issue the request var lRequestContent = new MultipartFormDataContent(); lRequestContent.Add(new StringContent(pFileName), "fileName"); lRequestContent.Add(new StringContent(pFile.Length.ToString()), "fileSize"); lClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", pToken); var lResponse = lClient.PostAsync($"{BaseUrl}/{BaseFolderPath}.initiateUpload.json", lRequestContent).Result; }

Has anyone experienced this before, and if so, do you have any advice you would be willing to share?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

1 reply

PhillipKoAuthor
Level 2
March 11, 2024

I think the 401 may be a result of the way the token is getting created.  If I use the developer console to generate a temp local token, the code works as expected.  I think I need to maybe adjust my scopes on the token request...