From dcb66c50075ef4e47b97b8434f59be1bbb21f47b Mon Sep 17 00:00:00 2001 From: RKrom Date: Sun, 9 Sep 2012 14:33:33 +0000 Subject: [PATCH] Added non anonymous access for Imgur, is still in an initial state. git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2015 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4 --- .../Forms/SettingsForm.Designer.cs | 15 +++ GreenshotImgurPlugin/ImgurConfiguration.cs | 6 ++ GreenshotImgurPlugin/ImgurUtils.cs | 92 +++++++++++++------ .../Languages/language_imgurplugin-en-US.xml | 3 + 4 files changed, 89 insertions(+), 27 deletions(-) diff --git a/GreenshotImgurPlugin/Forms/SettingsForm.Designer.cs b/GreenshotImgurPlugin/Forms/SettingsForm.Designer.cs index 26957dad0..4e6335483 100644 --- a/GreenshotImgurPlugin/Forms/SettingsForm.Designer.cs +++ b/GreenshotImgurPlugin/Forms/SettingsForm.Designer.cs @@ -53,6 +53,7 @@ namespace GreenshotImgurPlugin { this.combobox_uploadimageformat = new GreenshotPlugin.Controls.GreenshotComboBox(); this.label_upload_format = new GreenshotPlugin.Controls.GreenshotLabel(); this.historyButton = new GreenshotPlugin.Controls.GreenshotButton(); + this.checkbox_anonymous_access = new GreenshotPlugin.Controls.GreenshotCheckBox(); this.checkbox_usepagelink = new GreenshotPlugin.Controls.GreenshotCheckBox(); this.SuspendLayout(); // @@ -126,6 +127,18 @@ namespace GreenshotImgurPlugin { this.historyButton.UseVisualStyleBackColor = true; this.historyButton.Click += new System.EventHandler(this.ButtonHistoryClick); // + // checkbox_anonymous_access + // + this.checkbox_anonymous_access.AutoSize = true; + this.checkbox_anonymous_access.LanguageKey = "imgur.anonymous_access"; + this.checkbox_anonymous_access.Location = new System.Drawing.Point(15, 80); + this.checkbox_anonymous_access.Name = "checkbox_anonymous_access"; + this.checkbox_anonymous_access.PropertyName = "AnonymousAccess"; + this.checkbox_anonymous_access.SectionName = "Imgur"; + this.checkbox_anonymous_access.Size = new System.Drawing.Size(297, 17); + this.checkbox_anonymous_access.TabIndex = 13; + this.checkbox_anonymous_access.UseVisualStyleBackColor = true; + // // checkbox_usepagelink // this.checkbox_usepagelink.AutoSize = true; @@ -143,6 +156,7 @@ namespace GreenshotImgurPlugin { this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi; this.ClientSize = new System.Drawing.Size(387, 168); + this.Controls.Add(this.checkbox_anonymous_access); this.Controls.Add(this.checkbox_usepagelink); this.Controls.Add(this.historyButton); this.Controls.Add(this.label_upload_format); @@ -167,6 +181,7 @@ namespace GreenshotImgurPlugin { private GreenshotPlugin.Controls.GreenshotLabel label_url; private GreenshotPlugin.Controls.GreenshotButton buttonCancel; private GreenshotPlugin.Controls.GreenshotButton buttonOK; + private GreenshotPlugin.Controls.GreenshotCheckBox checkbox_anonymous_access; private GreenshotPlugin.Controls.GreenshotCheckBox checkbox_usepagelink; } } diff --git a/GreenshotImgurPlugin/ImgurConfiguration.cs b/GreenshotImgurPlugin/ImgurConfiguration.cs index 0eec9d764..d46728d75 100644 --- a/GreenshotImgurPlugin/ImgurConfiguration.cs +++ b/GreenshotImgurPlugin/ImgurConfiguration.cs @@ -43,6 +43,12 @@ namespace GreenshotImgurPlugin { public bool UploadReduceColors; [IniProperty("UsePageLink", Description = "Use pagelink instead of direct link on the clipboard", DefaultValue = "False")] public bool UsePageLink; + [IniProperty("AnonymousAccess", Description = "Use anonymous access to Imgur", DefaultValue="true")] + public bool AnonymousAccess; + [IniProperty("ImgurToken", Description = "The Imgur token", Encrypted=true, ExcludeIfNull=true)] + public string ImgurToken; + [IniProperty("ImgurTokenSecret", Description = "The Imgur token secret", Encrypted=true, ExcludeIfNull=true)] + public string ImgurTokenSecret; [IniProperty("ImgurUploadHistory", Description="Imgur upload history (ImgurUploadHistory.hash=deleteHash)")] public Dictionary ImgurUploadHistory; diff --git a/GreenshotImgurPlugin/ImgurUtils.cs b/GreenshotImgurPlugin/ImgurUtils.cs index c585212d4..6ba2bbc8e 100644 --- a/GreenshotImgurPlugin/ImgurUtils.cs +++ b/GreenshotImgurPlugin/ImgurUtils.cs @@ -132,38 +132,67 @@ namespace GreenshotImgurPlugin { // Add image uploadRequest.Append("image="); uploadRequest.Append(EscapeText(System.Convert.ToBase64String(imageData, 0, dataLength))); + // add type + uploadRequest.Append("&type=base64"); // add key - uploadRequest.Append("&"); - uploadRequest.Append("key="); + uploadRequest.Append("&key="); uploadRequest.Append(IMGUR_ANONYMOUS_API_KEY); // add title if (title != null) { - uploadRequest.Append("&"); - uploadRequest.Append("title="); + uploadRequest.Append("&title="); uploadRequest.Append(EscapeText(title)); } // add filename if (filename != null) { - uploadRequest.Append("&"); - uploadRequest.Append("name="); + uploadRequest.Append("&name="); uploadRequest.Append(EscapeText(filename)); } - string url = config.ImgurApiUrl + "/upload"; - HttpWebRequest webRequest = (HttpWebRequest)NetworkHelper.CreateWebRequest(url); - - webRequest.Method = "POST"; - webRequest.ContentType = "application/x-www-form-urlencoded"; - webRequest.ServicePoint.Expect100Continue = false; - - using(StreamWriter streamWriter = new StreamWriter(webRequest.GetRequestStream())) { - streamWriter.Write(uploadRequest.ToString()); - } + string url; string responseString; - using (WebResponse response = webRequest.GetResponse()) { - LogCredits(response); - Stream responseStream = response.GetResponseStream(); - StreamReader responseReader = new StreamReader(responseStream); - responseString = responseReader.ReadToEnd(); + + if (config.AnonymousAccess) { + url = config.ImgurApiUrl + "/upload"; + HttpWebRequest webRequest = (HttpWebRequest)NetworkHelper.CreateWebRequest(url); + + webRequest.Method = "POST"; + webRequest.ContentType = "application/x-www-form-urlencoded"; + webRequest.ServicePoint.Expect100Continue = false; + + using(StreamWriter streamWriter = new StreamWriter(webRequest.GetRequestStream())) { + streamWriter.Write(uploadRequest.ToString()); + } + using (WebResponse response = webRequest.GetResponse()) { + LogCredits(response); + Stream responseStream = response.GetResponseStream(); + StreamReader responseReader = new StreamReader(responseStream); + responseString = responseReader.ReadToEnd(); + } + + } else { + url = config.ImgurApiUrl + "/account/images"; + OAuthHelper oAuth = new OAuthHelper(); + oAuth.CallbackUrl = "http://getgreenshot.org"; + oAuth.AccessTokenUrl = "https://api.imgur.com/oauth/access_token"; + oAuth.AuthorizeUrl = "https://api.imgur.com/oauth/authorize"; + oAuth.RequestTokenUrl = "https://api.imgur.com/oauth/request_token"; + oAuth.ConsumerKey = "907d4455b8c38144d68c4f72190af4c40504a0ac7"; + oAuth.ConsumerSecret = "d33902ef409fea163ab755454c15b3d0"; + oAuth.UserAgent = "Greenshot"; + if (string.IsNullOrEmpty(config.ImgurToken)) { + LOG.Debug("Creating Imgur Token"); + oAuth.getRequestToken(); + if (string.IsNullOrEmpty(oAuth.authorizeToken("Imgur authorization"))) { + return null; + } + string accessToken = oAuth.getAccessToken(); + config.ImgurToken = oAuth.Token; + config.ImgurTokenSecret = oAuth.TokenSecret; + } else { + LOG.Debug("Using stored Imgur Token"); + oAuth.Token = config.ImgurToken; + oAuth.TokenSecret = config.ImgurTokenSecret; + } + responseString = oAuth.oAuthWebRequest(OAuthHelper.Method.POST, url, uploadRequest.ToString()); } LOG.Info(responseString); ImgurInfo imgurInfo = ImgurInfo.ParseResponse(responseString); @@ -262,7 +291,7 @@ namespace GreenshotImgurPlugin { } catch {} } - private static void ImgurOAuthExample() { + public static void ImgurOAuthExample() { OAuthHelper oAuth = new OAuthHelper(); oAuth.CallbackUrl = "http://getgreenshot.org"; oAuth.AccessTokenUrl = "https://api.imgur.com/oauth/access_token"; @@ -271,12 +300,21 @@ namespace GreenshotImgurPlugin { oAuth.ConsumerKey = "907d4455b8c38144d68c4f72190af4c40504a0ac7"; oAuth.ConsumerSecret = "d33902ef409fea163ab755454c15b3d0"; oAuth.UserAgent = "Greenshot"; - oAuth.getRequestToken(); - if (string.IsNullOrEmpty(oAuth.authorizeToken("Imgur authorization"))) { - return; + if (string.IsNullOrEmpty(config.ImgurToken)) { + LOG.Debug("Creating Imgur Token"); + oAuth.getRequestToken(); + if (string.IsNullOrEmpty(oAuth.authorizeToken("Imgur authorization"))) { + return; + } + string accessToken = oAuth.getAccessToken(); + config.ImgurToken = oAuth.Token; + config.ImgurTokenSecret = oAuth.TokenSecret; + } else { + LOG.Debug("Using stored Imgur Token"); + oAuth.Token = config.ImgurToken; + oAuth.TokenSecret = config.ImgurTokenSecret; } - string accessToken = oAuth.getAccessToken(); - MessageBox.Show(oAuth.oAuthWebRequest(OAuth.Method.GET, "http://api.imgur.com/2/account", null)); + System.Windows.Forms.MessageBox.Show(oAuth.oAuthWebRequest(OAuthHelper.Method.GET, "http://api.imgur.com/2/account", null)); } } } diff --git a/GreenshotImgurPlugin/Languages/language_imgurplugin-en-US.xml b/GreenshotImgurPlugin/Languages/language_imgurplugin-en-US.xml index f9b4e32a6..22eb3840e 100644 --- a/GreenshotImgurPlugin/Languages/language_imgurplugin-en-US.xml +++ b/GreenshotImgurPlugin/Languages/language_imgurplugin-en-US.xml @@ -37,6 +37,9 @@ Delete Imgur {0} + + Use anonymous access + Use page link instead of image link on clipboard