为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
森林海 2023-05-13 16:10:52
在 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 中使用相同的访问令牌并且它有效。
查看完整描述

1 回答

?
qq_花开花谢_0

TA贡献1835条经验 获得超6个赞

我会在 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();


查看完整回答
反对 回复 2023-05-13
  • 1 回答
  • 0 关注
  • 71 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信