简体   繁体   English

NetSuite OAUTH1 POST 请求在 Postman 中有效,但在 Restsharp 中失败

[英]NetSuite OAUTH1 POST request works in Postman but fails in Restsharp

I'm using RestSharp in .NET 6 to execute a POST request to NetSuite in a c# console application.我在 .NET 6 中使用 RestSharp 在 c# 控制台应用程序中执行对 NetSuite 的 POST 请求。 I'm using Token Based Authentication and OAuth1我正在使用基于令牌的身份验证和 OAuth1

When I execute the request using the same credentials (consumer key, consumer secret, access token, access token secret and realm) in C#, for GET requests, it works.当我在 C# 中使用相同的凭据(消费者密钥、消费者机密、访问令牌、访问令牌机密和领域)执行请求时,对于 GET 请求,它有效。 I'm able to authenticate and get a response.我能够进行身份验证并获得回复。

When I try a POST in C#, I get a 401, 'Unauthorized' with an error message stating that the token was rejected.当我在 C# 中尝试 POST 时,我收到 401“未授权”错误消息,指出令牌被拒绝。 The same POST request, with the same auth values and URL works in Postman however.但是,具有相同身份验证值和 URL 的相同 POST 请求在 Postman 中有效。

I feel like Postman is doing something to the authentication header in a different way to Restsharp, but that still doesn't explain why GET requests are working with RestSharp我觉得 Postman 正在以与 Restsharp 不同的方式对身份验证 header 做一些事情,但这仍然不能解释为什么 GET 请求与 RestSharp 一起工作

public string ExecuteRequest(string url, int httpMethod, string body = "")
        {
            var client = new RestClient(url);
            client.Authenticator = GetOAuth1Authenticator();
            Method method = (Method)httpMethod;
            var request = new RestRequest(url, method);
            client.AddDefaultHeader("Accept", "*/*");
            client.Options.MaxTimeout = -1;
            request.AddHeader("Cookie", "NS_ROUTING_VERSION=LAGGING");
            request.AddHeader("ContentType", "application/json");
            if (string.IsNullOrEmpty(body) == false)
            {
                request.AddParameter("application/json", body, ParameterType.RequestBody);
            }
            var response = client.Execute(request);

            if (response.IsSuccessful == false)
            {
                throw new HttpRequestException($"ERROR: {response.ErrorMessage} - RESPONSE CONTENT: {response.Content}");
            }
            if (response.Content == null)
            {
                throw new NullReferenceException("API RESPONSE IS NULL");
            }
            return response.Content;
        }

        private OAuth1Authenticator GetOAuth1Authenticator()
        {
            OAuth1Authenticator authenticator = OAuth1Authenticator.ForAccessToken(consumerKey: Credential.consumer_key,
                consumerSecret: Credential.consumer_secret,
                token: Credential.access_token, tokenSecret: Credential.access_token_secret, signatureMethod: RestSharp.Authenticators.OAuth.OAuthSignatureMethod.HmacSha256);
            authenticator.Realm = Credential.accountId;
            
            return authenticator;
        }

For anyone who knows SuiteTalk REST API for NetSuite, I'm trying to do a POST request to transform a PO into a VendorBill, using this endpoint: .netsuite host url]/purchaseOrder/{id}/!transform/vendorBill对于任何了解 NetSuite SuiteTalk REST API 的人,我正在尝试使用此端点执行 POST 请求以将 PO 转换为 VendorBill:.netsuite host url]/purchaseOrder/{id}/!transform/vendorBill

try尝试

var client = new RestClient(urlString);    
var request = new RestRequest(Method.POST);

btw, check your oauth method, when you are generating the signature you must specify the method you are using ("POST")顺便说一句,检查你的 oauth 方法,当你生成签名时你必须指定你正在使用的方法(“POST”)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GET 请求适用于 Postman,但使用 RestSharp 失败 - GET request works on Postman but fail with RestSharp 在 RestSharp 上获取请求失败并在 postman 上工作 - Get request failed on RestSharp and works on postman GET请求可在HttpWebRequest,Postman中使用。 但不在HttpClient或RestSharp中 - GET request works in HttpWebRequest,Postman . but not in HttpClient or RestSharp 我的 POST 请求在 c# 中失败,但在 POSTMAN 中运行良好 - My POST request fails in c# but works perfectly fine in POSTMAN 将调用工作放在PostMan中但不在RestSharp中:获取错误请求 - Put call works in PostMan but not in RestSharp: Getting a bad request 对 https://oauth2.googleapis.com/token 的发布请求在代码中一直失败,适用于 Postman - Post Request to https://oauth2.googleapis.com/token keeps failing in Code, Works on Postman 来自邮递员的POST请求未能通过模型​​验证 - POST request from Postman fails model validation 客户端/服务器握手使用 RestSharp 失败,但在 Postman 和使用 Fiddler 作为代理时工作正常。 证书长度 0 - Client / Server Handshake fails using RestSharp, but works fine on Postman and when using Fiddler as proxy. Certificate length 0 GET 请求在浏览器和 Postman 中有效,但是在使用 HttpClient 和 RestSharp 时我得到了未经授权 - GET request works in browser and Postman, but I get Unauthorized when using HttpClient and RestSharp RestSharp 返回 BadRequest 错误但 PostMan 工作正常 - RestSharp returns BadRequest error But PostMan works correctly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM