Esempio n. 1
0
		/// <summary>
		/// Do the actual upload to Flickr
		/// For more details on the available parameters, see: http://flickrnet.codeplex.com
		/// </summary>
		/// <param name="surfaceToUpload"></param>
		/// <param name="outputSettings"></param>
		/// <param name="title"></param>
		/// <param name="filename"></param>
		/// <returns>url to image</returns>
		public static string UploadToFlickr(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string title, string filename) {
			OAuthSession oAuth = new OAuthSession(FlickrCredentials.ConsumerKey, FlickrCredentials.ConsumerSecret);
			oAuth.BrowserSize = new Size(520, 800);
			oAuth.CheckVerifier = false;
			oAuth.AccessTokenUrl = FLICKR_ACCESS_TOKEN_URL;
			oAuth.AuthorizeUrl = FLICKR_AUTHORIZE_URL;
			oAuth.RequestTokenUrl = FLICKR_REQUEST_TOKEN_URL;
			oAuth.LoginTitle = "Flickr authorization";
			oAuth.Token = config.FlickrToken;
			oAuth.TokenSecret = config.FlickrTokenSecret;
			if (string.IsNullOrEmpty(oAuth.Token)) {
				if (!oAuth.Authorize()) {
					return null;
				}
				if (!string.IsNullOrEmpty(oAuth.Token)) {
					config.FlickrToken = oAuth.Token;
				}
				if (!string.IsNullOrEmpty(oAuth.TokenSecret)) {
					config.FlickrTokenSecret = oAuth.TokenSecret;
				}
				IniConfig.Save();
			}
			try {
				IDictionary<string, object> signedParameters = new Dictionary<string, object>();
				signedParameters.Add("content_type", "2");	// Screenshot
				signedParameters.Add("tags", "Greenshot");
				signedParameters.Add("is_public", config.IsPublic ? "1" : "0");
				signedParameters.Add("is_friend", config.IsFriend ? "1" : "0");
				signedParameters.Add("is_family", config.IsFamily ? "1" : "0");
				signedParameters.Add("safety_level", string.Format("{0}", (int)config.SafetyLevel));
				signedParameters.Add("hidden", config.HiddenFromSearch ? "1" : "2");
				IDictionary<string, object> otherParameters = new Dictionary<string, object>();
				otherParameters.Add("photo", new SurfaceContainer(surfaceToUpload, outputSettings, filename));
				string response = oAuth.MakeOAuthRequest(HTTPMethod.POST, FLICKR_UPLOAD_URL, signedParameters, otherParameters, null);
				string photoId = GetPhotoId(response);

				// Get Photo Info
				signedParameters = new Dictionary<string, object> { { "photo_id", photoId } };
				string photoInfo = oAuth.MakeOAuthRequest(HTTPMethod.POST, FLICKR_GET_INFO_URL, signedParameters, null, null);
				return GetUrl(photoInfo);
			} catch (Exception ex) {
				LOG.Error("Upload error: ", ex);
				throw;
			} finally {
				if (!string.IsNullOrEmpty(oAuth.Token)) {
					config.FlickrToken = oAuth.Token;
				}
				if (!string.IsNullOrEmpty(oAuth.TokenSecret)) {
					config.FlickrTokenSecret = oAuth.TokenSecret;
				}
			}
		}
Esempio n. 2
0
		public static string UploadToDropbox(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string filename) {
			OAuthSession oAuth = new OAuthSession(DropBoxCredentials.CONSUMER_KEY, DropBoxCredentials.CONSUMER_SECRET);
			oAuth.BrowserSize = new Size(1080, 650);
			oAuth.CheckVerifier = false;
			oAuth.AccessTokenUrl = "https://api.dropbox.com/1/oauth/access_token";
			oAuth.AuthorizeUrl = "https://api.dropbox.com/1/oauth/authorize";
			oAuth.RequestTokenUrl = "https://api.dropbox.com/1/oauth/request_token";
			oAuth.LoginTitle = "Dropbox authorization";
			oAuth.Token = config.DropboxToken;
			oAuth.TokenSecret = config.DropboxTokenSecret;

			try {
				SurfaceContainer imageToUpload = new SurfaceContainer(surfaceToUpload, outputSettings, filename);
				string uploadResponse = oAuth.MakeOAuthRequest(HTTPMethod.POST, "https://api-content.dropbox.com/1/files_put/sandbox/" + OAuthSession.UrlEncode3986(filename), null, null, imageToUpload);
				LOG.DebugFormat("Upload response: {0}", uploadResponse);
			} catch (Exception ex) {
				LOG.Error("Upload error: ", ex);
				throw;
			} finally {
				if (!string.IsNullOrEmpty(oAuth.Token)) {
					config.DropboxToken = oAuth.Token;
				}
				if (!string.IsNullOrEmpty(oAuth.TokenSecret)) {
					config.DropboxTokenSecret = oAuth.TokenSecret;
				}
			}

			// Try to get a URL to the uploaded image
			try {
				string responseString = oAuth.MakeOAuthRequest(HTTPMethod.GET, "https://api.dropbox.com/1/shares/sandbox/" + OAuthSession.UrlEncode3986(filename), null, null, null);
				if (responseString != null) {
					LOG.DebugFormat("Parsing output: {0}", responseString);
					IDictionary<string, object> returnValues = JSONHelper.JsonDecode(responseString);
					if (returnValues.ContainsKey("url")) {
						return returnValues["url"] as string;
					}
				}
			} catch (Exception ex) {
				LOG.Error("Can't parse response.", ex);
			}
			return null;
 		}
Esempio n. 3
0
        /// <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> uploadParameters = new Dictionary<string, object>();
            IDictionary<string, object> otherParameters = new Dictionary<string, object>();
            // add title
            if (title != null && config.AddTitle) {
                otherParameters.Add("title", title);
            }
            // add filename
            if (filename != null && config.AddFilename) {
                otherParameters.Add("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 = (HttpWebRequest)NetworkHelper.CreateWebRequest(config.ImgurApiUrl + "/upload.xml?" + NetworkHelper.GenerateQueryParameters(otherParameters));
                webRequest.Method = "POST";
                webRequest.ContentType = "image/" + outputSettings.Format.ToString();
                webRequest.ServicePoint.Expect100Continue = false;
                try {
                    using (var requestStream = webRequest.GetRequestStream()) {
                        ImageOutput.SaveToStream(surfaceToUpload, requestStream, outputSettings);
                    }

                    using (WebResponse response = webRequest.GetResponse()) {
                        using (StreamReader reader = new StreamReader(response.GetResponseStream(), true)) {
                            responseString = reader.ReadToEnd();
                        }
                        LogCredits(response);
                    }
                } catch (Exception ex) {
                    LOG.Error("Upload to imgur gave an exeption: ", ex);
                    throw;
                }
            } else {
                OAuthSession oAuth = new OAuthSession(ImgurCredentials.CONSUMER_KEY, ImgurCredentials.CONSUMER_SECRET);
                oAuth.BrowserSize = new Size(650, 500);
                oAuth.CallbackUrl = "http://getgreenshot.org";
                oAuth.AccessTokenUrl = "http://api.imgur.com/oauth/access_token";
                oAuth.AuthorizeUrl = "http://api.imgur.com/oauth/authorize";
                oAuth.RequestTokenUrl = "http://api.imgur.com/oauth/request_token";
                oAuth.LoginTitle = "Imgur authorization";
                oAuth.Token = config.ImgurToken;
                oAuth.TokenSecret = config.ImgurTokenSecret;
                if (string.IsNullOrEmpty(oAuth.Token)) {
                    if (!oAuth.Authorize()) {
                        return null;
                    }
                    if (!string.IsNullOrEmpty(oAuth.Token)) {
                        config.ImgurToken = oAuth.Token;
                    }
                    if (!string.IsNullOrEmpty(oAuth.TokenSecret)) {
                        config.ImgurTokenSecret = oAuth.TokenSecret;
                    }
                    IniConfig.Save();
                }
                try {
                    otherParameters.Add("image", new SurfaceContainer(surfaceToUpload, outputSettings, filename));
                    responseString = oAuth.MakeOAuthRequest(HTTPMethod.POST, "http://api.imgur.com/2/account/images.xml", uploadParameters, otherParameters, null);
                } catch (Exception ex) {
                    LOG.Error("Upload to imgur gave an exeption: ", ex);
                    throw;
                } finally {
                    if (oAuth.Token != null) {
                        config.ImgurToken = oAuth.Token;
                    }
                    if (oAuth.TokenSecret != null) {
                        config.ImgurTokenSecret = oAuth.TokenSecret;
                    }
                    IniConfig.Save();
                }
            }
            return ImgurInfo.ParseResponse(responseString);
        }
Esempio n. 4
0
 /// <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)
 {
     OAuthSession oAuth = new OAuthSession(PicasaCredentials.ConsumerKey, PicasaCredentials.ConsumerSecret);
     oAuth.BrowserSize = new Size(1020, 590);
     oAuth.AccessTokenUrl =  GoogleAccountUri + "OAuthGetAccessToken";
     oAuth.AuthorizeUrl =	GoogleAccountUri + "OAuthAuthorizeToken";
     oAuth.RequestTokenUrl = GoogleAccountUri + "OAuthGetRequestToken";
     oAuth.LoginTitle = "Picasa authorization";
     oAuth.Token = Config.PicasaToken;
     oAuth.TokenSecret = Config.PicasaTokenSecret;
     oAuth.RequestTokenParameters.Add("scope", "https://picasaweb.google.com/data/");
     oAuth.RequestTokenParameters.Add("xoauth_displayname", "Greenshot");
     if (string.IsNullOrEmpty(oAuth.Token)) {
         if (!oAuth.Authorize()) {
             return null;
         }
         if (!string.IsNullOrEmpty(oAuth.Token)) {
             Config.PicasaToken = oAuth.Token;
         }
         if (!string.IsNullOrEmpty(oAuth.TokenSecret)) {
             Config.PicasaTokenSecret = oAuth.TokenSecret;
         }
         IniConfig.Save();
     }
     try {
         IDictionary<string, string> headers = new Dictionary<string, string>();
         headers.Add("slug", OAuthSession.UrlEncode3986(filename));
         string response = oAuth.MakeOAuthRequest(HTTPMethod.POST, "https://picasaweb.google.com/data/feed/api/user/default/albumid/default", headers, null, null, new SurfaceContainer(surfaceToUpload, outputSettings, filename));
         return ParseResponse(response);
     } catch (Exception ex) {
         LOG.Error("Upload error: ", ex);
         throw;
     } finally {
         if (!string.IsNullOrEmpty(oAuth.Token)) {
             Config.PicasaToken = oAuth.Token;
         }
         if (!string.IsNullOrEmpty(oAuth.TokenSecret)) {
             Config.PicasaTokenSecret = oAuth.TokenSecret;
         }
     }
 }
Esempio n. 5
0
		/// <summary>
		/// Helper method to create an OAuth session object for contacting the Photobucket API
		/// </summary>
		/// <returns>OAuthSession</returns>
		private static OAuthSession createSession(bool autoLogin) {
			OAuthSession oAuth = new OAuthSession(PhotobucketCredentials.ConsumerKey, PhotobucketCredentials.ConsumerSecret);
			oAuth.AutoLogin = autoLogin;
			oAuth.CheckVerifier = false;
			// This url is configured in the Photobucket API settings in the Photobucket site!!
			oAuth.CallbackUrl = "http://getgreenshot.org";
			oAuth.AccessTokenUrl = "http://api.photobucket.com/login/access";
			oAuth.AuthorizeUrl = "http://photobucket.com/apilogin/login";
			oAuth.RequestTokenUrl = "http://api.photobucket.com/login/request";
			oAuth.BrowserSize = new Size(1010, 400);
			// Photobucket is very particular about the used methods!
			oAuth.RequestTokenMethod = HTTPMethod.POST;
			oAuth.AccessTokenMethod = HTTPMethod.POST;

			oAuth.LoginTitle = "Photobucket authorization";
			if (string.IsNullOrEmpty(config.SubDomain) || string.IsNullOrEmpty(config.Token) || string.IsNullOrEmpty(config.Username)) {
				if (!autoLogin) {
					return null;
				}
				if (!oAuth.Authorize()) {
					return null;
				}
				if (!string.IsNullOrEmpty(oAuth.Token)) {
					config.Token = oAuth.Token;
				}
				if (!string.IsNullOrEmpty(oAuth.TokenSecret)) {
					config.TokenSecret = oAuth.TokenSecret;
				}
				if (oAuth.AccessTokenResponseParameters != null && oAuth.AccessTokenResponseParameters["subdomain"] != null) {
					config.SubDomain = oAuth.AccessTokenResponseParameters["subdomain"];
				}
				if (oAuth.AccessTokenResponseParameters != null && oAuth.AccessTokenResponseParameters["username"] != null) {
					config.Username = oAuth.AccessTokenResponseParameters["username"];
				}
				IniConfig.Save();
			}
			oAuth.Token = config.Token;
			oAuth.TokenSecret = config.TokenSecret;
			return oAuth;
		}