通过 HttpClient 访问 Design Automation API v3 时出现 404

在 Postman 中运行对 Design Automation API 的调用工作得很好,但是当我尝试使用 HttpClient 在 C# 中进行相同的调用时,它们失败并显示 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 中使用相同的访问令牌并且它有效。


森林海
浏览 72回答 1
1回答

qq_花开花谢_0

我会在 HttpRequestMessage 中包含端点、标头和 httpmethod。然后发送并赋值给HttpResponseMessage。var client = new HttpClient{&nbsp; &nbsp; 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 clientrequest.Headers.Add(TokenType, AccessToken);// send the request, assign to responseHttpResponseMessage response = await client.SendAsync(request);//then, we can grab the data through the Contentstring result = await response.Content.ReadAsStringAsync();
打开App,查看更多内容
随时随地看视频慕课网APP