Nick Proud
Nick Proud

Reputation: 395

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. I'm using Token Based Authentication and 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. 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. The same POST request, with the same auth values and URL works in Postman however.

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

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

Upvotes: 3

Views: 631

Answers (1)

Paul Orozco
Paul Orozco

Reputation: 21

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")

Upvotes: 0

Related Questions