C# API Intergration

simon26simon26 Member Posts: 2
edited March 2 in Development

Hello,

I'm trying to get the api working with C#. I found some example on the internet but none of them work. Does any one have an example how to get a product and update a product?

Sincerly Simon

Answers

  • LucienVersendaalLucienVersendaal Moderator, Lightspeed Staff Posts: 119 moderator

    Hi {{ticket.requester.first_name}},

    Thank you for reaching out to us.

    Unfortunately we don't have any code example for C# this because most of the code will be written in PHP. But I found an article that might help https://www.codeproject.com/Questions/857719/How-to-integrate-LightSpeed-Cloud-Retail-API-in-Ne

    I hope this help.

  • simon26simon26 Member Posts: 2

    Hi

    I tried the following code:


    var url = $"https://{ApiKey}:{ApiSecret}@api.webshopapp.com/en/products/{lsProduct}.json";

          var request = (HttpWebRequest)WebRequest.Create(url);

          request.Method = "PUT";

          request.Credentials = new NetworkCredential(ApiKey, ApiSecret);

          request.PreAuthenticate = true;

          request.ContentType = "application/json";


          const string json = "\"product\":{\"title\":\"test\"}";


          using (var writer = new StreamWriter(request.GetRequestStream()))

          {

            writer.Write(json);

            writer.Flush();

            writer.Close();

          }


          var responseMessage = new HttpResponseMessage();


          using (var webResponse = (HttpWebResponse)request.GetResponse())

          {

            var stream = webResponse.GetResponseStream();

            if (stream == null) return;


            using (var reader = new StreamReader(stream))

            {

              var objText = reader.ReadToEnd();

              var objJson = (JObject)JsonConvert.DeserializeObject(objText);

              responseMessage.Content = new StringContent(objText);

            }

          }


    But I get 401 error. 'The remote server returned an error: (401) Unauthorized.'


    What can it be?

Sign In or Register to comment.