private string createSprdAuthHeader(string method, string url, string time) { string data = method + " " + url + " " + time; string sig = ApplicationLibary.sha1Generate(data, ApplicationLibary.SECRET); string header = "Authorization: SprdAuth apiKey=\"$apiKey\", data=\"$data\", sig=\"$sig\""; header = header.Replace("$apiKey", ApplicationLibary.API_KEY); header = header.Replace("$data", data); header = header.Replace("$sig", sig); return(header); }
private void UploadProgress() { foreach (var fileImage in lsImageFileNames) { try { //Tiêu đề dùng chung string text_All = Path.GetFileName(fileImage).Split('.')[0].ToString(); txtName.Invoke((MethodInvoker) delegate { txtName.Text = text_All; }); memoDescription.Invoke((MethodInvoker) delegate { memoDescription.Text = text_All; }); //cấu hình dữ liệu string image = fileImage; string title = text_All; // txtName.Text; string description = text_All; // memoDescription.Text; string tags = memoTag.Text; string shop = getSelectShop(); double amount = double.Parse(double.Parse(txtPrice.Text).ToString("N2")); if (!File.Exists(image)) { ApplicationLibary.writeLogThread(lsBoxLog, "File not found: " + Path.GetFileName(image), 1); continue; } #region -----------Step 1: Upload Image----------- ApplicationLibary.writeLogThread(lsBoxLog, "Uploadding " + Path.GetFileName(image), 3); string img_UrlUpload = User.USER_HREF + "/design-uploads"; var urlUploadImage = ApplicationLibary.encodeURL(url: img_UrlUpload, defaultParam: "createProductIdea=true", time: ApplicationLibary.getTimeStamp()); NameValueCollection nvc = new NameValueCollection(); var data = HttpUploadFile(urlUploadImage, image, "filedata", "image/png", nvc); if (int.Parse(data["status"].ToString()) == -1) { ApplicationLibary.writeLogThread(lsBoxLog, "Step 1: " + data["data"].ToString(), 3); continue; } JObject jObj = JObject.Parse(data["data"].ToString()); var designId = jObj["designId"].ToString(); var ideaId = jObj["ideaId"].ToString(); var name = jObj["name"].ToString(); #endregion #region -----------Step 2: Upload Data----------- string u_method = "PUT"; //https://partner.spreadshirt.com/api/v1/users/302721328/ideas/5a33342faa0c6d3e511164f3?apiKey=1c711bf5-b82d-40de-bea6-435b5473cf9b&locale=us_US&mediaType=json&sig=5a88e6520a13a9aa1f7b39036a7c120cd445ccab&time=1513305661500 string u_urlUpload = User.USER_HREF + "/ideas/" + ideaId; string u_time = ApplicationLibary.getTimeStamp(); string u_dataUrl = u_method + " " + u_urlUpload + " " + u_time + ""; string u_sig = ApplicationLibary.sha1Generate(u_dataUrl, ApplicationLibary.SECRET); Dictionary <string, object> dataObj = new Dictionary <string, object>(); dataObj.Add("amount", amount); dataObj.Add("shop", shop); dataObj.Add("ideaID", ideaId); dataObj.Add("designID", designId); dataObj.Add("title", title); dataObj.Add("description", description); dataObj.Add("tags", tags); dataObj.Add("sig", u_sig); string rs_UrlUpload = ApplicationLibary.encodeURL(url: u_urlUpload, method: u_method, locale: "us_US", mediaType: "json", time: u_time); string rs_Data2Send = refixData2Send(dataObj); HttpWebRequest wRequestUpload = (HttpWebRequest)WebRequest.Create(rs_UrlUpload); wRequestUpload.Headers.Add("Accept-Language", "vi-VN,vi;q=0.8,en-US;q=0.5,en;q=0.3"); wRequestUpload.Accept = "application/json, text/plain, */*"; wRequestUpload.Host = "partner.spreadshirt.com"; wRequestUpload.ContentType = "application/json;charset=utf-8"; wRequestUpload.Referer = "https://partner.spreadshirt.com/designs/" + ideaId; wRequestUpload.CookieContainer = cookieApplication; wRequestUpload.ServicePoint.Expect100Continue = false; wRequestUpload.ProtocolVersion = HttpVersion.Version11; wRequestUpload.Timeout = 90000; wRequestUpload.ReadWriteTimeout = 90000; wRequestUpload.KeepAlive = true; Dictionary <string, object> step2Upload = PutDataAPI(wRequestUpload, rs_Data2Send); if (int.Parse(step2Upload["status"].ToString()) == -1) { ApplicationLibary.writeLogThread(lsBoxLog, "Step 2: " + step2Upload["data"].ToString(), 1); continue; } var objUploadEnd = JObject.Parse(step2Upload["data"].ToString()); var linkIdea = objUploadEnd["href"].ToString(); #endregion #region -----------Step 3: Publish----------- //https://partner.spreadshirt.com/api/v1/users/302721328/ideas/5a339e2aaa0c6d3e511e3268/publishingDetails?apiKey=1c711bf5-b82d-40de-bea6-435b5473cf9b&locale=us_US&mediaType=json&sig=ff531cb9b45015934d699c796dc013033dcff8e8&time=1513333983186 var urlPublish = User.USER_HREF + "/ideas/" + ideaId + "/publishingDetails"; string p_urlPublish = ApplicationLibary.encodeURL(url: urlPublish, method: "PUT", locale: "us_US", mediaType: "json"); var p_data2SendPublish = @"{""list"": [" + dataCYOID; foreach (string str in getPublishingDetailsPublish(shop)) { var x = str.Replace(@"\", ""); p_data2SendPublish += "," + x; } p_data2SendPublish = p_data2SendPublish.TrimEnd(',') + "]}"; HttpWebRequest wRequestPublish = (HttpWebRequest)WebRequest.Create(p_urlPublish); wRequestPublish.Headers.Add("Accept-Language", "vi-VN,vi;q=0.8,en-US;q=0.5,en;q=0.3"); wRequestPublish.Accept = "application/json, text/plain, */*"; wRequestPublish.Host = "partner.spreadshirt.com"; wRequestPublish.ContentType = "application/json;charset=utf-8"; wRequestPublish.Referer = "https://partner.spreadshirt.com/designs/" + ideaId; wRequestPublish.CookieContainer = cookieApplication; Dictionary <string, object> step3Publish = PutDataAPI(wRequestPublish, p_data2SendPublish); if (int.Parse(step3Publish["status"].ToString()) == -1) { ApplicationLibary.writeLogThread(lsBoxLog, "Step 3: " + step3Publish["data"].ToString(), 1); continue; } ApplicationLibary.writeLogThread(lsBoxLog, "Upload & Publish finish: " + "https://partner.spreadshirt.com/designs/" + ideaId, 1); #endregion MoveFileUploaded(image); } catch (Exception ex) { ApplicationLibary.writeLogThread(lsBoxLog, "Upload Error: " + ex.Message, 1); } } }