Click here to Skip to main content
15,906,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

I developed the console application for upload the base 64 document and its running in daily 5 minutes which is configured on windows task scheduler ,Sometime I'm encountering an authorization issue while trying to perform a specific function on the API calls. I would appreciate any help or insights you can provide regarding this error.

Error Message:
Authorization failed. You are not authorized to perform this function.

Context:
I'm currently working on integrating our application with the Hexagon EAM  using the  REST API. I'm attempting to make a request to the "upload document" endpoint in order to upload a new document.

Authentication Method:
I'm using basic authentication to authenticate my requests. I'm providing the username and password in the Authorization header using the "Basic" scheme. Here's a code snippet showing how I'm setting the headers:
C#
using (HttpClient attclient = new HttpClient())
{
    var apiPayload = "";
    var attContent = new StringContent(apiPayload, Encoding.UTF8, "application/json");
    attclient.BaseAddress = new Uri(apiUrl);
    // Set custom header
    attclient.DefaultRequestHeaders.Add("Organization", organization);
    attclient.DefaultRequestHeaders.Add("Tenant", tenant);
    // Set basic authentication credentials
    var authValue  = Encoding.ASCII.GetBytes(userName + ":" + password);    
    attclient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
        Convert.ToBase64String(authValue));    
    // Send POST request to the API    
    attclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    HttpResponseMessage response = attclient.PostAsync(apiUrl, attContent).Result;
    // Check if the request was successful
    if (response.IsSuccessStatusCode)
    {
        // Read the response content
        var responseBody = await response.Content.ReadAsStringAsync();
        Console.WriteLine(responseBody);
    }
    else
    {
        Console.WriteLine("Request failed with status code: " + response.StatusCode);
    }
}

Expected Behavior: I expect the API to upload the document with the provided details and return a success response with the newly upload document`s information.

Additional Information: I have verified that the provided username and password are correct. I have also double-checked the API documentation to ensure that I'm using the correct endpoint and the required fields in the request payload.

Help Needed: I would greatly appreciate any assistance or insights you can provide to help me troubleshoot this authorization issue. If you have encountered a similar problem or have any suggestions on what might be causing this error, please share your thoughts.

Thank you in advance for your time and help!

Best regards,

Ajaz Khan

What I have tried:

I have verified that the provided username and password are correct. I have also double-checked the API documentation to ensure that I'm using the correct endpoint and the required fields in the request payload.
Posted
Updated 25-May-23 21:44pm
v2
Comments
Graeme_Grant 26-May-23 1:55am    
Many API service use Refresh Tokens for security reasons. Is this mentioned in their documentation? If so, you need to add support.

1 solution

You need to talk to the API creators: they will have a lot more knowledge of their product that we would.

The error message is pretty explicit: your current user is not authorised to do whatever it is you are asking the API to do. The only way to fix that is to find out what authorisation your user needs, and find out how you get it.

Sorry, but we can't help you with any of that.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900