3

I am trying to get WooCommerce data using WooCommerce v3 API from c#.

When I use below url in browser I get the products data as json;

https://MyUsername:MyPassword@mydomain.com/wp-json/wc/v3/products?consumer_key=ck_12345678901234567890&consumer_secret=cs_12345678901234567890

When I use url

https://MyUsername:MyPassword@mydomain.com/wp-json/wc/v3/products

and

consumer_key = ck_12345678901234567890 and consumer_secret = cs_12345678901234567890

as Params in Postman then I get the products data as well. So the credentials and url are valid.

However when I try to do this using HttpWebRequest it fails with "The remote server returned an error: (401) Unauthorized" exception. My code is below and the exception appears on the last line of code.

The vales in HttpWebRequest before the GetResponseAsync call can be seen here.

What am I doing wrong?

Thanks

Regards

string wc_url = "https://MyUsername:MyPassword@mydomain.com/wp-json/wc/v3/products?consumer_key=ck_12345678901234567890&consumer_secret=cs_12345678901234567890";
HttpWebRequest httpWebRequest = null;

httpWebRequest = (HttpWebRequest)WebRequest.Create(wc_url);
httpWebRequest.AllowReadStreamBuffering = false;
    
WebResponse wr = await httpWebRequest.GetResponseAsync().ConfigureAwait(false);
Y U
  • 65
  • 5
  • [Have you found this?](https://stackoverflow.com/questions/46569291/reportviewer-error-401-view-user-logging-in) – Jiale Xue - MSFT Oct 22 '21 at 05:53
  • This one worked; Dim client = New RestClient("https://example.org/wp-json/wc/v3/products") client.Timeout = -1 client.AddDefaultQueryParameter("consumer_key", "ck_12345678901234567890") client.AddDefaultQueryParameter("consumer_secret", "cs_12345678901234567890") client.Authenticator = New HttpBasicAuthenticator(MyUsername, MyPassword) Dim request = New RestRequest(Method.[GET]) Dim response As IRestResponse = client.Execute(request) – Y U Oct 23 '21 at 06:48

0 Answers0