static void updateInventory(int listingId, string sellPrice, string currentQty, string sku) { var client = new RestClient(); client.BaseUrl = new Uri("https://openapi.etsy.com/v2/listings/" + listingId + "/inventory?api_key=3ptctueuc44gh9e3sny1oix5&write_missing_inventory=true"); var request = new RestRequest(Method.GET); IRestResponse response = client.Execute(request); CheckRequestThrottleLimit(); try { var inventoryVariations = JsonConvert.DeserializeObject <GetInventory>(response.Content); if (inventoryVariations.results.products.Count == 1) { List <UpdateInventory> updateInventoryList = new List <UpdateInventory>(); UpdateInventory updateInventory = new UpdateInventory(); updateInventory.product_id = inventoryVariations.results.products[0].product_id; updateInventory.offerings.Add(new UpdateOffering()); updateInventory.offerings[0].offering_id = inventoryVariations.results.products[0].offerings[0].offering_id; updateInventory.offerings[0].price = (Convert.ToDouble(sellPrice) * 1.15).ToString(); updateInventory.offerings[0].quantity = int.Parse(currentQty); updateInventory.sku = sku; updateInventoryList.Add(updateInventory); var client1 = new RestClient("https://openapi.etsy.com/v2/listings/" + listingId + "/inventory"); var request1 = new RestRequest(Method.PUT); request1.AddHeader("Authorization", "OAuth " + OAuthSignatureGenerator.GetAuthorizationHeaderValue(client1.BaseUrl, JsonConvert.SerializeObject(updateInventoryList))); request1.AddHeader("Content-Type", "application/x-www-form-urlencoded"); request1.AddParameter("products", JsonConvert.SerializeObject(updateInventoryList)); IRestResponse response1 = client1.Execute(request1); CheckRequestThrottleLimit(); //WriteToFile("Stock Item is Updated with Listing ID: " + listingId + " (" + sku + ")"); Listings = Listings + sku + ", "; //WriteToFile(response1.Content); } } catch (Exception e) { //WriteToFile("Error for " + listingId + "-----" + e.StackTrace); } }
public string GetAuthorizationHeaderValue(string accessToken, string accessTokenSecret, string url, HttpMethod httpMethod) { /*5.2. Consumer Request Parameters * * OAuth Protocol Parameters are sent from the Consumer to the Service Provider in one of three methods, in order of decreasing preference: * * In the HTTP Authorization header as defined in OAuth HTTP Authorization Scheme. * As the HTTP POST request body with a content-type of application/x-www-form-urlencoded. * Added to the URLs in the query part (as defined by [RFC3986] section 3). * In addition to these defined methods, future extensions may describe alternate methods for sending the OAuth Protocol Parameters. The methods for sending other request parameters are left undefined, but SHOULD NOT use the OAuth HTTP Authorization Scheme header. * */ /* 5.4.1. Authorization Header * * The OAuth Protocol Parameters are sent in the Authorization header the following way: * * Parameter names and values are encoded per Parameter Encoding. * For each parameter, the name is immediately followed by an ‘=’ character (ASCII code 61), a ‘”’ character (ASCII code 34), the parameter value (MAY be empty), and another ‘”’ character (ASCII code 34). * Parameters are separated by a comma character (ASCII code 44) and OPTIONAL linear whitespace per [RFC2617]. * The OPTIONAL realm parameter is added and interpreted per [RFC2617], section 1.2. * For example: * * Authorization: OAuth realm="http://sp.example.com/", * oauth_consumer_key="0685bd9184jfhq22", * oauth_token="ad180jjd733klru7", * oauth_signature_method="HMAC-SHA1", * oauth_signature="wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D", * oauth_timestamp="137131200", * oauth_nonce="4572616e48616d6d65724c61686176", * oauth_version="1.0" * */ var nonce = GetNonce(); var timeStamp = GetTimeStamp(); nonce = "81446751"; timeStamp = "1586807203"; var requestParameters = new List <string> { "oauth_consumer_key=" + _config.ConsumerKey, "oauth_token=" + accessToken, "oauth_signature_method=HMAC-SHA1", "oauth_timestamp=" + timeStamp, "oauth_nonce=" + nonce, "products=[{\"product_id\":4177710601,\"property_values\":[],\"offerings\":[{\"offering_id\":4389645380,\"price\":\"1339\",\"quantity\":10}]}]" }; var requestUri = new Uri(url, UriKind.Absolute); if (!string.IsNullOrWhiteSpace(requestUri.Query)) { var parameters = ExtractQueryParameters(requestUri.Query); foreach (var kvp in parameters) { requestParameters.Add(kvp.Key + "=" + kvp.Value); } } url = GetNormalizedUrl(requestUri); // Appendix A.5.1. Generating Signature Base String var signatureBaseString = GetSignatureBaseString(httpMethod.ToString().ToUpper(), url, requestParameters); // Appendix A.5.2. Calculating Signature Value var signature = OAuthSignatureGenerator.GenerateSignature(new Uri("https://openapi.etsy.com/v2/listings/792035687/inventory"), "PUT", _config.ConsumerKey, _config.ConsumerSecret, nonce, timeStamp, OAuthSignatureGenerator.SignatureMethod.HmacSha1, accessToken, accessTokenSecret, "1.0"); //var signature = GetSignature(signatureBaseString, _config.ConsumerSecret, accessTokenSecret); // Same as request parameters but uses a quote (") character around its values and is comma separated var requestParametersForHeader = new List <string> { "oauth_consumer_key=\"" + _config.ConsumerKey + "\"", "oauth_token=\"" + accessToken + "\"", "oauth_signature_method=\"HMAC-SHA1\"", "oauth_timestamp=\"" + timeStamp + "\"", "oauth_nonce=\"" + nonce + "\"", "oauth_version=\"1.0\"", "oauth_signature=\"" + Uri.EscapeDataString(signature) + "\"" }; return(ConcatList(requestParametersForHeader, ",")); }
static void PollActiveListings() { Console.WriteLine(); Console.WriteLine("Active State Polling Started"); int pageNo = 5; while (true) { var client = new RestClient(); ShopListings shopListing = new ShopListings(); client.BaseUrl = new System.Uri("https://openapi.etsy.com/v2/shops/maahira/listings/active?limit=" + pageLimit + "&page=" + pageNo++); var request = new RestRequest(Method.GET); string oauth = "OAuth " + OAuthSignatureGenerator.GetAuthorizationHeaderValue(client.BaseUrl, "", "GET"); request.AddHeader("Authorization", oauth); IRestResponse response = client.Execute(request); CheckRequestThrottleLimit(); shopListing = JsonConvert.DeserializeObject <ShopListings>(response.Content); if (shopListing.count != null && shopListing.count > 0 && shopListing.results.Count > 0) { foreach (Listing listingItem in shopListing.results) { if (IsSkuPresent(listingItem)) { if (listingItem.sku.Count > 1) { if (listingItem.state == "edit" || listingItem.state == "active") { if (listingItem.state == "edit") { changeInventoryState(listingItem.listing_id, "active"); } updateMultiItemInventory(listingItem.listing_id); } } else { //if (!listingItem.description.ToLower().Contains("sku")) //{ // updateDescriptionWithSku(listingItem); // continue; //} //listingItem.sku[0] //continue; var client1 = new RestClient("https://www.silvercityonline.com/stock/src/scripts/getItemData.php?perPage=50&page=1&itemNo=" + listingItem.sku[0] + "&sdt=0000-00-00&edt=0000-00-00"); var request1 = new RestRequest(Method.GET); IRestResponse response1 = client1.Execute(request1); CheckRequestThrottleLimit(); if (response1.Content.Contains(getDataStart)) { int startInd = response1.Content.IndexOf(getDataStart); string substr = response1.Content.Substring(startInd + getDataStart.Length); ItemData stockItem = new ItemData(); stockItem = JsonConvert.DeserializeObject <ItemData>(substr.Substring(0, substr.IndexOf("}") + 1)); if ((listingItem.state == "edit" || listingItem.state == "active") && int.Parse(stockItem.curStock) > 0) { if (listingItem.state == "edit") { changeInventoryState(listingItem.listing_id, "active"); } updateInventory(listingItem.listing_id, stockItem.sellPrice, stockItem.curStock, stockItem.itemNo); } else if (int.Parse(stockItem.curStock) != listingItem.quantity || double.Parse(listingItem.price) != double.Parse(stockItem.sellPrice) * 1.15) { if (double.Parse(stockItem.sellPrice) == 0) { stockItem.sellPrice = listingItem.price; } if (int.Parse(stockItem.curStock) > 0) { updateInventory(listingItem.listing_id, stockItem.sellPrice, stockItem.curStock, stockItem.itemNo); } else if (listingItem.state != "edit") { changeInventoryState(listingItem.listing_id, "inactive"); } } } } } } Console.WriteLine("Done for Page " + (pageNo - 1) + " @ " + DateTime.Now); } else { pageNo = 1; break; } } Console.WriteLine(); Console.WriteLine("Active State Polling Done"); }
static void updateMultiItemInventory(int listingId) { var client = new RestClient(); client.BaseUrl = new Uri("https://openapi.etsy.com/v2/listings/" + listingId + "/inventory?write_missing_inventory=true"); var request = new RestRequest(Method.GET); request.AddHeader("Authorization", "OAuth " + OAuthSignatureGenerator.GetAuthorizationHeaderValue(client.BaseUrl, "", "GET")); IRestResponse response = client.Execute(request); try { string sku = ""; var inventoryVariations = JsonConvert.DeserializeObject <GetInventory>(response.Content); dynamic data = JObject.Parse(response.Content); foreach (var item in data.results.price_on_property.Children()) { inventoryVariations.price_on_property = item.Value; } foreach (var item in data.results.quantity_on_property.Children()) { inventoryVariations.quantity_on_property = item.Value; } foreach (var item in data.results.sku_on_property.Children()) { inventoryVariations.sku_on_property = item.Value; } UpdateInventory1 updateInventoryList = new UpdateInventory1(); foreach (Product p in inventoryVariations.results.products) { ItemData latestProductData = getItemDataFromPanel(p.sku); if (!String.IsNullOrEmpty(latestProductData.itemNo)) { if (double.Parse(latestProductData.sellPrice) > 0) { p.offerings[0].price = (Convert.ToDouble(latestProductData.sellPrice) * 1.15).ToString(); } if (int.Parse(latestProductData.curStock) > 0) { p.offerings[0].quantity = int.Parse(latestProductData.curStock); } else { p.offerings[0].quantity = 0; } } sku += p.sku + ","; updateInventoryList.Products.Add(p); } var client1 = new RestClient("https://openapi.etsy.com/v2/listings/" + listingId + "/inventory"); var request1 = new RestRequest(Method.PUT); request1.AddHeader("Content-Type", "application/x-www-form-urlencoded"); OAuthProperties properties = OAuthSignatureGenerator.GetMultiProductAuthorizationHeaderValue(client1.BaseUrl, JsonConvert.SerializeObject(updateInventoryList.Products), JsonConvert.SerializeObject(inventoryVariations.price_on_property), JsonConvert.SerializeObject(inventoryVariations.quantity_on_property), JsonConvert.SerializeObject(inventoryVariations.sku_on_property), "PUT"); request1.AddParameter("oauth_consumer_key", properties.oauth_consumer_key); request1.AddParameter("oauth_token", properties.oauth_token); request1.AddParameter("oauth_signature_method", properties.oauth_signature_method); request1.AddParameter("oauth_timestamp", properties.oauth_timestamp); request1.AddParameter("oauth_nonce", properties.oauth_nonce); request1.AddParameter("oauth_version", properties.oauth_version); request1.AddParameter("oauth_signature", properties.oauth_signature); request1.AddParameter("products", JsonConvert.SerializeObject(updateInventoryList.Products)); request1.AddParameter("price_on_property", JsonConvert.SerializeObject(inventoryVariations.price_on_property)); request1.AddParameter("quantity_on_property", JsonConvert.SerializeObject(inventoryVariations.quantity_on_property)); request1.AddParameter("sku_on_property", JsonConvert.SerializeObject(inventoryVariations.sku_on_property)); IRestResponse response1 = client1.Execute(request1); CheckRequestThrottleLimit(); Console.WriteLine("Stock Item is Updated with Listing ID: " + listingId + " (" + sku + ")"); Listings = Listings + sku + ", "; } catch (Exception e) { Console.WriteLine("Error for " + listingId + "-----" + e.StackTrace); } }