BIM 360现场API问题V1:GET请求与Postman完美配合,使用RestSharp响应“未经授权”

时间:2019-07-18 11:35:42

标签: api field postman restsharp autodesk-bim360

尝试在我的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中建立了一个示例Get Request。与表单数据,x-www-form-urlencoded(无身份验证,承载令牌)完美配合。 试图将确切的Restshap请求代码从Postman复制到我的C#应用​​程序中。不起作用;始终以“未经授权”回应。尝试更改cookie mngmt和身份验证窗口/ 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?

2 个答案:

答案 0 :(得分:0)

  1. 对于您的Cookie,您需要在客户端上设置CookieContainer。

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

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

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

答案 1 :(得分: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); 

希望有帮助!