Esempio n. 1
0
        public static string UploadToDropbox(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string filename)
        {
            var oAuth = new OAuthSession(DropBoxCredentials.CONSUMER_KEY, DropBoxCredentials.CONSUMER_SECRET)
            {
                BrowserSize     = new Size(1080, 650),
                CheckVerifier   = false,
                AccessTokenUrl  = "https://api.dropbox.com/1/oauth/access_token",
                AuthorizeUrl    = "https://api.dropbox.com/1/oauth/authorize",
                RequestTokenUrl = "https://api.dropbox.com/1/oauth/request_token",
                LoginTitle      = "Dropbox authorization",
                Token           = DropboxConfig.DropboxToken,
                TokenSecret     = DropboxConfig.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))
                {
                    DropboxConfig.DropboxToken = oAuth.Token;
                }
                if (!string.IsNullOrEmpty(oAuth.TokenSecret))
                {
                    DropboxConfig.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. 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);
                    DropboxResponse response = JSONSerializer <DropboxResponse> .Deserialize(responseString);

                    return(response.url);
                }
            } catch (Exception ex) {
                LOG.Error("Can't parse response.", ex);
            }
            return(null);
        }
Esempio n. 3
0
        /// <summary>
        /// Get list of photobucket albums
        /// </summary>
        /// <returns>List of string</returns>
        public static IList <string> RetrievePhotobucketAlbums()
        {
            if (_albumsCache != null)
            {
                return(_albumsCache);
            }
            string responseString;

            OAuthSession oAuth = CreateSession(false);

            if (oAuth == null)
            {
                return(null);
            }
            IDictionary <string, object> signedParameters = new Dictionary <string, object>();

            try {
                string apiUrl = $"http://api.photobucket.com/album/{PhotobucketConfig.Username}";
                responseString = oAuth.MakeOAuthRequest(HTTPMethod.GET, apiUrl, apiUrl.Replace("api.photobucket.com", PhotobucketConfig.SubDomain), signedParameters, null, null);
            } catch (Exception ex) {
                Log.Error("Error uploading to Photobucket.", ex);
                throw;
            } finally {
                if (!string.IsNullOrEmpty(oAuth.Token))
                {
                    PhotobucketConfig.Token = oAuth.Token;
                }
                if (!string.IsNullOrEmpty(oAuth.TokenSecret))
                {
                    PhotobucketConfig.TokenSecret = oAuth.TokenSecret;
                }
            }
            if (responseString == null)
            {
                return(null);
            }
            try {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(responseString);
                List <string> albums  = new List <string>();
                var           xmlNode = doc.GetElementsByTagName("content").Item(0);
                if (xmlNode != null)
                {
                    RecurseAlbums(albums, null, xmlNode.ChildNodes);
                }
                Log.DebugFormat("Albums: {0}", string.Join(",", albums.ToArray()));
                _albumsCache = albums;
                return(albums);
            } catch (Exception e) {
                Log.Error("Error while Reading albums: ", e);
            }

            Log.Debug("Upload to Photobucket was finished");
            return(null);
        }
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>
        /// 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. 6
0
        /// <summary>
        /// Do the actual upload to Photobucket
        /// For more details on the available parameters, see: http://api.Photobucket.com/resources_anon
        /// </summary>
        /// <returns>PhotobucketResponse</returns>
        public static PhotobucketInfo UploadToPhotobucket(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string albumPath, string title, string filename)
        {
            string responseString;

            if (string.IsNullOrEmpty(albumPath))
            {
                albumPath = "!";
            }

            OAuthSession oAuth = createSession(true);

            if (oAuth == null)
            {
                return(null);
            }
            IDictionary <string, object> signedParameters = new Dictionary <string, object>();

            // add album
            if (albumPath == null)
            {
                signedParameters.Add("id", config.Username);
            }
            else
            {
                signedParameters.Add("id", albumPath);
            }
            // add type
            signedParameters.Add("type", "image");
            // add title
            if (title != null)
            {
                signedParameters.Add("title", title);
            }
            // add filename
            if (filename != null)
            {
                signedParameters.Add("filename", filename);
            }
            IDictionary <string, object> unsignedParameters = new Dictionary <string, object>();

            // Add image
            unsignedParameters.Add("uploadfile", new SurfaceContainer(surfaceToUpload, outputSettings, filename));
            try {
                string apiUrl = "http://api.photobucket.com/album/!/upload";
                responseString = oAuth.MakeOAuthRequest(HTTPMethod.POST, apiUrl, apiUrl.Replace("api.photobucket.com", config.SubDomain), signedParameters, unsignedParameters, null);
            } catch (Exception ex) {
                LOG.Error("Error uploading to Photobucket.", ex);
                throw;
            } finally {
                if (!string.IsNullOrEmpty(oAuth.Token))
                {
                    config.Token = oAuth.Token;
                }
                if (!string.IsNullOrEmpty(oAuth.TokenSecret))
                {
                    config.TokenSecret = oAuth.TokenSecret;
                }
            }
            if (responseString == null)
            {
                return(null);
            }
            LOG.Info(responseString);
            PhotobucketInfo PhotobucketInfo = PhotobucketInfo.FromUploadResponse(responseString);

            LOG.Debug("Upload to Photobucket was finished");
            return(PhotobucketInfo);
        }
Esempio n. 7
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)
        {
            var oAuth = new OAuthSession(FlickrCredentials.ConsumerKey, FlickrCredentials.ConsumerSecret)
            {
                BrowserSize     = new Size(520, 800),
                CheckVerifier   = false,
                AccessTokenUrl  = FLICKR_ACCESS_TOKEN_URL,
                AuthorizeUrl    = FLICKR_AUTHORIZE_URL,
                RequestTokenUrl = FLICKR_REQUEST_TOKEN_URL,
                LoginTitle      = "Flickr authorization",
                Token           = config.FlickrToken,
                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", $"{(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. 8
0
        /// <summary>
        /// Do the actual upload to Flickr
        /// For more details on the available parameters, see: http://flickrnet.codeplex.com
        /// </summary>
        /// <param name="imageData">byte[] with image data</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  = "http://api.flickr.com/services/oauth/access_token";
            oAuth.AuthorizeUrl    = "http://api.flickr.com/services/oauth/authorize";
            oAuth.RequestTokenUrl = "http://api.flickr.com/services/oauth/request_token";
            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, "http://api.flickr.com/services/upload/", signedParameters, otherParameters, null);
                string photoId  = GetPhotoId(response);

                // Get Photo Info
                signedParameters = new Dictionary <string, object>();
                signedParameters.Add("photo_id", photoId);
                string photoInfo = oAuth.MakeOAuthRequest(HTTPMethod.POST, "http://api.flickr.com/services/rest/?method=flickr.photos.getInfo", 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;
                }
            }
        }