RestSharp OAuth1 Method.Options will not authenticate with NetSuite REST

58 Views Asked by At

The TestRequest function fails with RestSharp version 110.2.0. I get "Unauthorized","status":401,"o:errorDetails":[{"detail":"Invalid login attempt.

The difference between the 2 functions is the Method.Options and the end point. The 2nd function also has a body. I can not get the TestRequest code to authenticate. However the GetCustomListStoreSection function works just fine with the same authentication. I can get both to work fine in Postman and can get both to work using HttpClient.

I was hoping to move to RestSharp since it has method that is Synchronous. Yes I really do not want an Async request for this process. It is a queue manager and the requests have to happen in order and must wait for the 1st to finish before moving to the next.

Using .NET Framework 4.7.2/Winforms/C#/Visual Studio 2022

 public string TestRequest()
 {
     //Logging.WriteTrace("TestRequest");
     var url = _config.ApiRoot + "/*";

     var authenticator = OAuth1Authenticator.ForAccessToken(
         _config.ClientId, 
         _config.ClientSecret, 
         _config.TokenId, 
         _config.TokenSecret, 
         OAuthSignatureMethod.HmacSha256);
     authenticator.Realm = _config.AccountId;                                     

     var options = new RestClientOptions(url);
     options.Authenticator = authenticator;
     var client = new RestClient(options);
     var httpRequest = new RestRequest(url, Method.Options);                
     httpRequest.AddHeader("prefer", "transient");
     //httpRequest.AddBody(content);  There is no body on this test request.
     var httpResponse = client.Execute(httpRequest);               

     if (httpResponse.StatusCode == System.Net.HttpStatusCode.NoContent)
     {
         return "Success";
     }
     else
     {
         return httpResponse.Content;
     }
 }        

 public dynamic GetCustomListStoreSection()
 {
     var url = _config.ApiRoot + "/query/v1/suiteql";
     var authenticator = OAuth1Authenticator.ForAccessToken(
         _config.ClientId,
         _config.ClientSecret,
         _config.TokenId,
         _config.TokenSecret,
         OAuthSignatureMethod.HmacSha256);
     authenticator.Realm = _config.AccountId;
 
     var content = "{\"q\":\"Select id, name from CUSTOMLISTSTORESECTION where isinactive = 'F'\"}";

     var options = new RestClientOptions(url);
     options.Authenticator = authenticator;
     var client = new RestClient(options);
     var httpRequest = new RestRequest(url, Method.Post);
     httpRequest.AddHeader("prefer", "transient");
     httpRequest.AddBody(content);
     var httpResponse = client.Execute(httpRequest);

     if (httpResponse.StatusCode == System.Net.HttpStatusCode.OK)
     {                    
         dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(httpResponse.Content);
         return obj;
     }
     else
     {
         return null;
     }
 }

I tried authenticating with NetSuite using RestSharp using an Http.Options request.

0

There are 0 best solutions below