/// <summary> /// Put string /// </summary> /// <param name="url"></param> /// <param name="content"></param> /// <param name="settings">OAuth2Settings</param> /// <returns>response</returns> public static string HttpPut(string url, string content, OAuth2Settings settings) { var webRequest = OAuth2Helper.CreateOAuth2WebRequest(HTTPMethod.PUT, url, settings); byte[] data = Encoding.UTF8.GetBytes(content); using (var requestStream = webRequest.GetRequestStream()) { requestStream.Write(data, 0, data.Length); } return(NetworkHelper.GetResponseAsString(webRequest)); }
/// <summary> /// Do the actual upload to Box /// For more details on the available parameters, see: http://developers.box.net/w/page/12923951/ApiFunction_Upload%20and%20Download /// </summary> /// <param name="image">Image for box upload</param> /// <param name="title">Title of box upload</param> /// <param name="filename">Filename of box upload</param> /// <returns>url to uploaded image</returns> public static string UploadToBox(SurfaceContainer image, string title, string filename) { // Fill the OAuth2Settings var settings = new OAuth2Settings { AuthUrlPattern = "https://app.box.com/api/oauth2/authorize?client_id={ClientId}&response_type=code&state={State}&redirect_uri={RedirectUrl}", TokenUrl = "https://api.box.com/oauth2/token", CloudServiceName = "Box", ClientId = BoxCredentials.ClientId, ClientSecret = BoxCredentials.ClientSecret, RedirectUrl = "https://www.box.com/home/", BrowserSize = new Size(1060, 600), AuthorizeMode = OAuth2AuthorizeMode.EmbeddedBrowser, RefreshToken = Config.RefreshToken, AccessToken = Config.AccessToken, AccessTokenExpires = Config.AccessTokenExpires }; // Copy the settings from the config, which is kept in memory and on the disk try { var webRequest = OAuth2Helper.CreateOAuth2WebRequest(HTTPMethod.POST, UploadFileUri, settings); IDictionary <string, object> parameters = new Dictionary <string, object>(); parameters.Add("file", image); parameters.Add("parent_id", Config.FolderId); NetworkHelper.WriteMultipartFormData(webRequest, parameters); var response = NetworkHelper.GetResponseAsString(webRequest); Log.DebugFormat("Box response: {0}", response); var upload = JsonSerializer.Deserialize <Upload>(response); if (upload?.Entries == null || upload.Entries.Count == 0) { return(null); } if (Config.UseSharedLink) { string filesResponse = HttpPut(string.Format(FilesUri, upload.Entries[0].Id), "{\"shared_link\": {\"access\": \"open\"}}", settings); var file = JsonSerializer.Deserialize <FileEntry>(filesResponse); return(file.SharedLink.Url); } return($"http://www.box.com/files/0/f/0/1/f_{upload.Entries[0].Id}"); } finally { // Copy the settings back to the config, so they are stored. Config.RefreshToken = settings.RefreshToken; Config.AccessToken = settings.AccessToken; Config.AccessTokenExpires = settings.AccessTokenExpires; Config.IsDirty = true; IniConfig.Save(); } }
/// <summary> /// Do the actual upload to Picasa /// </summary> /// <param name="surfaceToUpload">Image to upload</param> /// <param name="outputSettings"></param> /// <param name="title"></param> /// <param name="filename"></param> /// <returns>PicasaResponse</returns> public static string UploadToPicasa(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string title, string filename) { // Fill the OAuth2Settings var settings = new OAuth2Settings { AuthUrlPattern = AuthUrl, TokenUrl = TokenUrl, CloudServiceName = "Picasa", ClientId = PicasaCredentials.ClientId, ClientSecret = PicasaCredentials.ClientSecret, AuthorizeMode = OAuth2AuthorizeMode.LocalServer, RefreshToken = Config.RefreshToken, AccessToken = Config.AccessToken, AccessTokenExpires = Config.AccessTokenExpires }; // Copy the settings from the config, which is kept in memory and on the disk try { var webRequest = OAuth2Helper.CreateOAuth2WebRequest(HTTPMethod.POST, string.Format(UploadUrl, Config.UploadUser, Config.UploadAlbum), settings); if (Config.AddFilename) { webRequest.Headers.Add("Slug", NetworkHelper.EscapeDataString(filename)); } SurfaceContainer container = new SurfaceContainer(surfaceToUpload, outputSettings, filename); container.Upload(webRequest); string response = NetworkHelper.GetResponseAsString(webRequest); return(ParseResponse(response)); } finally { // Copy the settings back to the config, so they are stored. Config.RefreshToken = settings.RefreshToken; Config.AccessToken = settings.AccessToken; Config.AccessTokenExpires = settings.AccessTokenExpires; Config.IsDirty = true; IniConfig.Save(); } }
/// <summary> /// Do the actual upload to Imgur /// For more details on the available parameters, see: http://api.imgur.com/resources_anon /// </summary> /// <param name="surfaceToUpload">ISurface to upload</param> /// <param name="outputSettings">OutputSettings for the image file format</param> /// <param name="title">Title</param> /// <param name="filename">Filename</param> /// <returns>ImgurInfo with details</returns> public static ImgurInfo UploadToImgur(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string title, string filename) { IDictionary <string, object> otherParameters = new Dictionary <string, object>(); // add title if (title != null && Config.AddTitle) { otherParameters["title"] = title; } // add filename if (filename != null && Config.AddFilename) { otherParameters["name"] = filename; } string responseString = null; if (Config.AnonymousAccess) { // add key, we only use the other parameters for the AnonymousAccess //otherParameters.Add("key", IMGUR_ANONYMOUS_API_KEY); HttpWebRequest webRequest = NetworkHelper.CreateWebRequest(Config.ImgurApi3Url + "/upload.xml?" + NetworkHelper.GenerateQueryParameters(otherParameters), HTTPMethod.POST); webRequest.ContentType = "image/" + outputSettings.Format; webRequest.ServicePoint.Expect100Continue = false; SetClientId(webRequest); try { using (var requestStream = webRequest.GetRequestStream()) { ImageOutput.SaveToStream(surfaceToUpload, requestStream, outputSettings); } using (WebResponse response = webRequest.GetResponse()) { LogRateLimitInfo(response); var responseStream = response.GetResponseStream(); if (responseStream != null) { using (StreamReader reader = new StreamReader(responseStream, true)) { responseString = reader.ReadToEnd(); } } } } catch (Exception ex) { Log.Error("Upload to imgur gave an exeption: ", ex); throw; } } else { var oauth2Settings = new OAuth2Settings { AuthUrlPattern = AuthUrlPattern, TokenUrl = TokenUrl, RedirectUrl = "https://imgur.com", CloudServiceName = "Imgur", ClientId = ImgurCredentials.CONSUMER_KEY, ClientSecret = ImgurCredentials.CONSUMER_SECRET, AuthorizeMode = OAuth2AuthorizeMode.EmbeddedBrowser, BrowserSize = new Size(680, 880), RefreshToken = Config.RefreshToken, AccessToken = Config.AccessToken, AccessTokenExpires = Config.AccessTokenExpires }; // Copy the settings from the config, which is kept in memory and on the disk try { var webRequest = OAuth2Helper.CreateOAuth2WebRequest(HTTPMethod.POST, Config.ImgurApi3Url + "/upload.xml", oauth2Settings); otherParameters["image"] = new SurfaceContainer(surfaceToUpload, outputSettings, filename); NetworkHelper.WriteMultipartFormData(webRequest, otherParameters); responseString = NetworkHelper.GetResponseAsString(webRequest); } finally { // Copy the settings back to the config, so they are stored. Config.RefreshToken = oauth2Settings.RefreshToken; Config.AccessToken = oauth2Settings.AccessToken; Config.AccessTokenExpires = oauth2Settings.AccessTokenExpires; Config.IsDirty = true; IniConfig.Save(); } } if (string.IsNullOrEmpty(responseString)) { return(null); } return(ImgurInfo.ParseResponse(responseString)); }