C# API Intergration

simon26simon26 Member Posts: 4
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: 256 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: 4

    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?

  • LucienVersendaalLucienVersendaal Moderator, Lightspeed Staff Posts: 256 moderator

    Hi @simon26,

    When I look at the collection in postman, I can generate example codes showing in different languages how to build the code. I did this with product update (PUT). Below is an example. If I compare this with your code, I miss the headers, which is why you also get a 401 un-authorized.

    var client = new RestClient("https://{{cluster}}/{{language}}/products/{product_id}.json");
    client.Timeout = -1;
    var request = new RestRequest(Method.PUT);
    request.AddHeader("Authorization", "{{token}}");
    request.AddHeader("Content-Type", "application/json");
    request.AddParameter("application/json", "{\n  \"product\": {\n    \"visibility\": \"hidden\",\n    \"data01\": \"\",\n    \"data02\": \"\",\n    \"data03\": \"\",\n    \"title\": \"Wade Crewneck Navyblue\",\n    \"fulltitle\": \"Wade Crewneck Navyblue\",\n    \"description\": \"De Wade Crewneck Navyblue van Wemoto. Deze blauwe trui met de enorme W op de chest heeft een heel klassieke look.\",\n    \"content\": \"De Wade Crewneck Navyblue van Wemoto. Deze blauwe trui met de enorme W op de chest heeft een heel klassieke look.\",\n    \"deliverydate\": 1,\n    \"supplier\": 2\n  }\n}",  ParameterType.RequestBody);
    IRestResponse response = client.Execute(request);
    Console.WriteLine(response.Content);
    

    Can you add the headers and retry this?

  • john_2084john_2084 Member Posts: 20
    edited May 18

    @simon26 ,

    I am doing all my Lightspeed interaction in C#.

    My first suggestion is to use RestSharp for your calls.

    I don't know if this helps, here is some code I use to interact with the LS endpoints.

    RestRequest request = new RestRequest("Item/" + itemId + "/.json");


    request.AddHeader("Authorization", "Bearer " + accessToken);

    RestClient client = new RestClient("https://api.lightspeedapp.com/API/Account/" + accountId + "/");


    IRestResponse response = client.Get(request);

    String json = response.Content;

    if ( response.IsSuccessful ) {

    ItemResponse = JsonConvert.DeserializeObject<ItemResponse>(json);


    }

  • simon26simon26 Member Posts: 4

    Hi all,

    I tried the postman tutorial from lightspeed. Filled in all the needed data. Below is the response.

    [code]
    {
        "error": {
            "code": 401,
            "method": "READ",
            "request": "/nl/shop.json",
            "message": "Could not authenticate you."
        }
    }
    

    This is the basic request. Still doesn't work. Any ideas?

  • john_2084john_2084 Member Posts: 20

    Is your account id correct? If so, your token is either not correct or you didn't set the header.

  • simon26simon26 Member Posts: 4

    Where can you put your Account ID in Postman? Can't find any option for it. I did set the API key's.


  • DonyDony Member Posts: 10

    Hi floks,

    I am also trying to access data from Lightspeed and get into our system. I want to do it as a task scheduler. I am so confused in registration process.

    I tried to follow steps given here : https://developers.lightspeedhq.com/retail/authentication/authentication-overview/


    I want to automate this integartion, but I am not sure I can do it or not. second step of taking temporary token is for everytime or one time that is also not sure from my side.


    please do let me know if anybody has any idea about it.

Sign In or Register to comment.