0

在 Postman 中运行对 Design Automation API 的调用工作得很好,但是当我尝试在 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 是结果的内容。我在 Postman 中使用相同的访问令牌并且它可以工作。

4

1 回答 1

0

我会在 HttpRequestMessage 中封装端点、标头和 httpmethod。然后将其发送并分配给 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();
于 2019-06-27T19:42:12.233 回答