将Postman Body表单数据参数转换为RestSharp C#POST请求

时间:2018-12-31 03:44:51

标签: c# api postman restsharp

谁会知道如何将下面的Postman POST Body表单数据参数转换为RestSharp C#POST请求,到目前为止我没有尝试过。终点是使用OAuth 1.0,我只能使用Body表单数据成功发布信息,而其他Body选项则无法成功。

enter image description here

邮递员RestSharp翻译成:

var client = new RestClient("https://openapi.etsy.com/v2/shipping/templates");
        var request2 = new RestRequest(Method.POST);
        request2.AddHeader("cache-control", "no-cache");
        request2.AddHeader("authorization", "OAuth oauth_consumer_key=" + consumerKey + ",oauth_token=" + oAuthToken + ",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp="+ timeStamp + ",oauth_nonce=" + nonce +",oauth_version=\"1.0\",oauth_signature=" + sig + "");
        request2.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
        request2.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"title\"\r\n\r\ntest title\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"origin_country_id\"\r\n\r\n209\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"primary_cost\"\r\n\r\n1\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"secondary_cost\"\r\n\r\n1\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);
        IRestResponse response2 = client.Execute(request2);

但是这不适用于RestSharp,它会抛出StatusCode Forbidden和内容'oauth_problem = signature_invalid ...'

如何使其起作用?参数只能与Body表单数据一起传递

更新:用于生成随机数,时间戳,签名等的代码:

            OAuthBase oAuth = new OAuthBase();

        string nonce = oAuth.GenerateNonce();
        string timeStamp = oAuth.GenerateTimeStamp();
        string normalizedUrl;
        string normalizedRequestParameters;

        string relativeUri = "url";
        string sig = oAuth.GenerateSignature(new Uri(BASE_URL.ToString() + relativeUri), consumerKey, consumerSecret, oAuthToken, oAuthTokenSecret, RequestType.POST.ToString(), timeStamp, nonce, out normalizedUrl, out normalizedRequestParameters);

0 个答案:

没有答案