404 при доступе к Design Automation API v3 через HttpClient - PullRequest
0 голосов
/ 27 июня 2019

Выполнение вызовов API автоматизации проектирования в Postman работает просто отлично, но когда я пытаюсь сделать те же вызовы в C # с помощью HttpClient, они завершаются неудачно с 404, который, кажется, фактически скрывает ошибку аутентификации:

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

Эта ссылка приводит к ошибке аутентификации:

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

Я следую примерам использования HttpClient, но я могу что-то упустить.Я успешно получил токен доступа, запустил

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);

, затем

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

, и вышеуказанный json является содержимым результата.Я использую тот же токен доступа в Почтальоне, и он работает.

1 Ответ

0 голосов
/ 27 июня 2019

Я бы обернул конечную точку, заголовки и метод http в сообщении HttpRequestMessage.Затем отправьте его и назначьте 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();
...