Search code examples
c#autodesk-designautomation

404 when accessing Design Automation API v3 through HttpClient


Running calls to the Design Automation API in Postman works just fine but when I try to make the same calls in C# using HttpClient they fail with a 404 that seems to actually hide an authentication error:

{ 
    "developerMessage":"The requested resource does not exist.",
    "userMessage":"",
    "errorCode":"ERR-002",
    "more info":"http://developer.api.autodesk.com/documentation/v1/errors/err-002"
}

That link leads to an authentication error:

<Error>
    <Code>AccessDenied</Code>
    <Message>Access Denied</Message>
    <RequestId>1F52E60A45AEF429</RequestId>
    <HostId>
        [ Some base64 ]
    </HostId>
</Error>

I'm following examples for how to use HttpClient, but I may be missing something. I successfully get the access token, run

var client = new HttpClient
{
    BaseAddress = new Uri("https://developer.api.autodesk.com/da/us-east")
};
client.DefaultRequestHeaders.Authorization =
            new System.Net.Http.Headers.AuthenticationHeaderValue(TokenType, AccessToken);

then

var result = await client.GetAsync("/v3/forgeapps/me");

and the above json is the result's content. I use the same access token in Postman and it works.


Solution

  • I would wrap up the endpoint, headers, and httpmethod in the HttpRequestMessage. Then send it and assign it to HttpResponseMessage.

    var client = new HttpClient
    {
        BaseAddress = new Uri("https://developer.api.autodesk.com/da/us-east/")
    };
    
    //throw the endpoint and HttpMethod here. Could also be HttpMethod.Post/Put/Delete (for your future reference)
    var request = new HttpRequestMessage(HttpMethod.Get, "v3/forgeapps/me");
    
    //also maybe try throwing the headers in with the request instead of the client
    request.Headers.Add(TokenType, AccessToken);
    
    // send the request, assign to response
    HttpResponseMessage response = await client.SendAsync(request);
    
    //then, we can grab the data through the Content
    string result = await response.Content.ReadAsStringAsync();