question

78669366 avatar image
0 Votes"
78669366 asked Bruce-SqlWork answered

401 error accessing valid rest api in c#

Hi

I am trying to access rest api from c#.

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

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 in c# it fails with "The remote server returned an error: (401) Unauthorized" exception. My c# code is below and the exception appears on the last line of code.

The values in HttpWebRequest before the GetResponseAsync call can be seen in attachment. 142366-httpwebrequest.pdf

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




dotnet-csharp
httpwebrequest.pdf (619.4 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi 78669366,

Look at this it works in Postman and url, but not in C#..
Please let me know if it works.

Best Regards,
Jiale Xue

0 Votes 0 ·

Hi

Thanks but using ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 did not seem to have work.

Regards

0 Votes 0 ·
Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered

Entering the username and password in the the url is a browser feature. The browser removes the values from the url before making the request, and passes them as basic authentication headers.

So to convert to webclient, remove from url and use basic authentication.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

78669366 avatar image
0 Votes"
78669366 answered

Hi

Using RestSharp below worked fine.

         Dim client = New RestClient("https://mydomain.com/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)


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.