Expand my Community achievements bar.

Calling AEM REST API always results in 401 Unauthorized

Avatar

Level 1

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?

1 Reply

Avatar

Level 1

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