[英]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.