0

尝试在我的 C# 解决方案中实现var client = new RestClient("https://bim360field.autodesk.com/fieldapi/checklists/v1/06eede44-a707-4f0b-9529-78abea6e6bf5"); var request = new RestRequest(Method.GET); https://bim360field.autodesk.com/apidoc/index.html#fieldapi/checklists/v1/checklists_api_method_2 。

我已经在 Postman 中设置了一个示例获取请求。与表单数据、x-www-form-urlencoded、无身份验证、不记名令牌完美配合。试图将确切的 Restshap 请求代码从 Postman 复制到我的 C# 应用程序。不工作;总是以“未经授权”响应。尝试更改 cookie mngmt 和 auth window/nltm mngmt。没有帮助。

var request = new RestRequest(Method.GET);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Connection", "keep-alive");
request.AddHeader("Content-Length", "91");
request.AddHeader("Accept-Encoding", "gzip, deflate");
request.AddHeader("Cookie", "OPTOUTMULTI_REF=b7027893-8564-4b33-8f1e-de0c175d38c6; OPTOUTMULTI_GEO=NL; utag_main=v_id:016639c85215001e1ab9f566c61201090006808800bd0$_sn:4$_ss:0$_st:1545042741235$ses_id:1545040922510%3Bexp-session$_pn:1%3Bexp-session; XSRF-TOKEN=YxM2BdzM19rML7OjKGZqcD0hAqv%2F225McEm8oB3m7No%3D; 75da74d446a8376e2cf6a286ff63573ab705f04c36540fdda21c1dbc1928d4e72cabf30a63e339690e88fc3801a93e44e6e3ddc20651f602d2c5eaaf49b21d61=f795a05c211085d994bf2e08996ff747");
request.AddHeader("Host", "bim360field.autodesk.com");
request.AddHeader("Postman-Token", "0b3c652d-462c-4aba-a661-476309792018,b4d4846c-a928-406f-837c-f16809a9bccd");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Accept", "*/*");
request.AddHeader("User-Agent", "PostmanRuntime/7.15.2");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"ticket\"\r\n\r\n{API_TICKET}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"project_id\"\r\n\r\n049bf984-b8d1-4330-a17c-0832c6facf49\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);```

I expect that this code is also working with Restsharp. Do I miss anything?
4

2 回答 2

0
  1. 对于您的 cookie,您需要在客户端上设置一个 CookieContainer。

    Client = new RestClient(Request.GetEnvironment()) { CookieContainer = new System.Net.CookieContainer() };

  2. 您不需要邮递员令牌。您将需要使用 API 提供的 Bearer Token 进行调用并将其添加到您的标头或参数中。

  3. 初始化 RestClient 时,您只想传递基本 url。然后在 RestRequest 初始化期间传递其余的 api 路径。当希望使用相同的 cookie 达到多个端点时,这将有助于将来使用。

于 2019-07-18T13:23:50.180 回答
0

这是一个适合您的工作示例:

// (1) Build request 
var client = new RestClient();
client.BaseUrl = new System.Uri("https://bim360field.autodesk.com/");

// Set resource or end point 
var request = new RestRequest();
request.Resource = "/fieldapi/checklists/v1";
request.Method = Method.GET;

// Add parameters 
request.AddParameter("ticket", "{API_TICKET}");
request.AddParameter("project_id", "049bf984-b8d1-4330-a17c-0832c6facf49");

// (2) Execute request and get response
IRestResponse response = client.Execute(request); 

希望能帮助到你!

于 2019-07-19T02:38:45.930 回答