Esempio n. 1
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. 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);
        }