【问题标题】:404 when accessing Design Automation API v3 through HttpClient通过 HttpClient 访问 Design Automation API v3 时出现 404
【发布时间】:2019-06-28 03:47:31
【问题描述】:

在 Postman 中运行对设计自动化 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 中使用相同的访问令牌并且它可以工作。

【问题讨论】:

    标签: c# autodesk-designautomation


    【解决方案1】:

    我会在 HttpRequestMessage 中封装端点、标头和 http 方法。然后将其发送并分配给 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();
    

    【讨论】:

    • 必须做 request.Headers.Add("Authorization",$"{TokenType} {AccessToken}");但它确实奏效了。现在只是想弄清楚原因。
    • 哇 / 需要在 BaseAddress 的末尾而不是在 get 端点的开头。感谢您的帮助!
    • 哈哈哈!好的!很高兴它对你有效!电脑又怪又挑剔
    猜你喜欢
    • 1970-01-01
    • 2020-09-22
    • 2023-03-17
    • 2018-03-31
    • 2018-10-30
    • 1970-01-01
    • 2021-07-26
    • 2020-04-04
    • 2023-03-07
    相关资源
    最近更新 更多