From b64de88b1ec5ffde6e1f452d8246cccd20f61295 Mon Sep 17 00:00:00 2001 From: Robin Date: Fri, 6 Nov 2015 14:12:30 +0100 Subject: [PATCH] Making the imgur destination more stable, unfortunately Imgur has issues... --- GreenshotImgurPlugin/ImgurUtils.cs | 74 ++++++++++++++++++--------- GreenshotPlugin/Core/NetworkHelper.cs | 3 +- GreenshotPlugin/Core/OAuthHelper.cs | 16 +++--- appveyor.yml | 4 +- 4 files changed, 65 insertions(+), 32 deletions(-) diff --git a/GreenshotImgurPlugin/ImgurUtils.cs b/GreenshotImgurPlugin/ImgurUtils.cs index ce24270c1..26c786b58 100644 --- a/GreenshotImgurPlugin/ImgurUtils.cs +++ b/GreenshotImgurPlugin/ImgurUtils.cs @@ -35,7 +35,7 @@ namespace GreenshotImgurPlugin { private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(ImgurUtils)); private const string SmallUrlPattern = "http://i.imgur.com/{0}s.jpg"; private static readonly ImgurConfiguration Config = IniConfig.GetIniSection(); - + private const string ImgurAnonymousClientId = "60e8838e21d6b66"; /// /// Load the complete history of the imgur uploads, with the corresponding information /// @@ -94,8 +94,8 @@ namespace GreenshotImgurPlugin { /// Use this to make sure Imgur knows from where the upload comes. /// /// - private static void SetClientId(HttpWebRequest webRequest) { - webRequest.Headers.Add("Authorization", "Client-ID " + ImgurCredentials.CONSUMER_KEY); + private static void SetClientId(HttpWebRequest webRequest, string clientId) { + webRequest.Headers.Add("Authorization", "Client-ID " + clientId); } /// @@ -126,7 +126,7 @@ namespace GreenshotImgurPlugin { webRequest.ContentType = "image/" + outputSettings.Format; webRequest.ServicePoint.Expect100Continue = false; - SetClientId(webRequest); + SetClientId(webRequest, ImgurAnonymousClientId); try { using (var requestStream = webRequest.GetRequestStream()) { ImageOutput.SaveToStream(surfaceToUpload, requestStream, outputSettings); @@ -152,17 +152,27 @@ namespace GreenshotImgurPlugin { oAuth.LoginTitle = "Imgur authorization"; oAuth.Token = Config.ImgurToken; oAuth.TokenSecret = Config.ImgurTokenSecret; - if (string.IsNullOrEmpty(oAuth.Token)) { - if (!oAuth.Authorize()) { - return null; + try + { + if (string.IsNullOrEmpty(oAuth.Token)) { + if (!oAuth.Authorize()) { + return null; + } + StoreOAuthToken(oAuth); } - if (!string.IsNullOrEmpty(oAuth.Token)) { - Config.ImgurToken = oAuth.Token; + } + catch (Exception ex) + { + // Retry + LOG.Error(ex); + if (string.IsNullOrEmpty(oAuth.Token)) + { + if (!oAuth.Authorize()) + { + return null; + } + StoreOAuthToken(oAuth); } - if (!string.IsNullOrEmpty(oAuth.TokenSecret)) { - Config.ImgurTokenSecret = oAuth.TokenSecret; - } - IniConfig.Save(); } try { otherParameters.Add("image", new SurfaceContainer(surfaceToUpload, outputSettings, filename)); @@ -171,18 +181,36 @@ namespace GreenshotImgurPlugin { 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(); + StoreOAuthToken(oAuth); } } return ImgurInfo.ParseResponse(responseString); } + /// + /// Helper method to store the OAuth output, also if the key is null we need to remove it + /// + /// OAuthSession + private static void StoreOAuthToken(OAuthSession oAuth) + { + bool hasChances = false; + + if (oAuth.Token != Config.ImgurToken) + { + Config.ImgurToken = oAuth.Token; + hasChances = true; + } + if (oAuth.TokenSecret != Config.ImgurTokenSecret) + { + Config.ImgurTokenSecret = oAuth.TokenSecret; + hasChances = true; + } + if (hasChances) + { + IniConfig.Save(); + } + } + /// /// Retrieve the thumbnail of an imgur image /// @@ -195,7 +223,7 @@ namespace GreenshotImgurPlugin { LOG.InfoFormat("Retrieving Imgur image for {0} with url {1}", imgurInfo.Hash, imgurInfo.SmallSquare); HttpWebRequest webRequest = NetworkHelper.CreateWebRequest(string.Format(SmallUrlPattern, imgurInfo.Hash), HTTPMethod.GET); webRequest.ServicePoint.Expect100Continue = false; - SetClientId(webRequest); + SetClientId(webRequest, ImgurCredentials.CONSUMER_KEY); using (WebResponse response = webRequest.GetResponse()) { LogRateLimitInfo(response); Stream responseStream = response.GetResponseStream(); @@ -217,7 +245,7 @@ namespace GreenshotImgurPlugin { LOG.InfoFormat("Retrieving Imgur info for {0} with url {1}", hash, url); HttpWebRequest webRequest = NetworkHelper.CreateWebRequest(url, HTTPMethod.GET); webRequest.ServicePoint.Expect100Continue = false; - SetClientId(webRequest); + SetClientId(webRequest, ImgurCredentials.CONSUMER_KEY); string responseString; try { using (WebResponse response = webRequest.GetResponse()) { @@ -251,7 +279,7 @@ namespace GreenshotImgurPlugin { string url = Config.ImgurApiUrl + "/delete/" + imgurInfo.DeleteHash; HttpWebRequest webRequest = NetworkHelper.CreateWebRequest(url, HTTPMethod.GET); webRequest.ServicePoint.Expect100Continue = false; - SetClientId(webRequest); + SetClientId(webRequest, ImgurCredentials.CONSUMER_KEY); string responseString; using (WebResponse response = webRequest.GetResponse()) { LogRateLimitInfo(response); diff --git a/GreenshotPlugin/Core/NetworkHelper.cs b/GreenshotPlugin/Core/NetworkHelper.cs index 82937517d..dc816eab7 100644 --- a/GreenshotPlugin/Core/NetworkHelper.cs +++ b/GreenshotPlugin/Core/NetworkHelper.cs @@ -482,8 +482,9 @@ namespace GreenshotPlugin.Core { } catch (WebException e) { response = (HttpWebResponse) e.Response; - HttpStatusCode statusCode = response.StatusCode; + HttpStatusCode statusCode = HttpStatusCode.Unused; if (response != null) { + statusCode = response.StatusCode; LOG.ErrorFormat("HTTP error {0}", statusCode); string errorContent = GetResponseAsString(response); if (alsoReturnContentOnError) diff --git a/GreenshotPlugin/Core/OAuthHelper.cs b/GreenshotPlugin/Core/OAuthHelper.cs index d744caced..110736f5e 100644 --- a/GreenshotPlugin/Core/OAuthHelper.cs +++ b/GreenshotPlugin/Core/OAuthHelper.cs @@ -476,7 +476,8 @@ namespace GreenshotPlugin.Core { /// /// Get the request token using the consumer key and secret. Also initializes tokensecret /// - private void GetRequestToken() { + /// response, this doesn't need to be used!! + private string GetRequestToken() { IDictionary parameters = new Dictionary(); foreach(var value in _requestTokenParameters) { parameters.Add(value); @@ -493,15 +494,17 @@ namespace GreenshotPlugin.Core { TokenSecret = _requestTokenResponseParameters[OAUTH_TOKEN_SECRET_KEY]; } } + return response; } /// /// Authorize the token by showing the dialog /// + /// Pass the response from the server's request token, so if there is something wrong we can show it. /// The request token. - private string GetAuthorizeToken() { + private string GetAuthorizeToken(string requestTokenResponse) { if (string.IsNullOrEmpty(Token)) { - Exception e = new Exception("The request token is not set"); + Exception e = new Exception("The request token is not set, service responded with: " + requestTokenResponse); throw e; } LOG.DebugFormat("Opening AuthorizationLink: {0}", AuthorizationLink); @@ -567,13 +570,14 @@ namespace GreenshotPlugin.Core { TokenSecret = null; Verifier = null; LOG.Debug("Creating Token"); - try { - GetRequestToken(); + string requestTokenResponse; + try { + requestTokenResponse = GetRequestToken(); } catch (Exception ex) { LOG.Error(ex); throw new NotSupportedException("Service is not available: " + ex.Message); } - if (string.IsNullOrEmpty(GetAuthorizeToken())) { + if (string.IsNullOrEmpty(GetAuthorizeToken(requestTokenResponse))) { LOG.Debug("User didn't authenticate!"); return false; } diff --git a/appveyor.yml b/appveyor.yml index 4cb63923d..4d9cffd6c 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -27,9 +27,9 @@ environment: credentials_flickr_consumer_secret: secure: 9TthlljPHXWPkDDeG3uiFVJ9YJwHZOV0ZsojaIBBuvw= credentials_imgur_consumer_key: - secure: XRTg1Ecs6ER9m4779CJAng== + secure: z8S4QZ3/InPe3dgCf0CNyS0VGKuRyjjP8WMAq+AkK5OZJxZcbIxwobjgelE5CWYL credentials_imgur_consumer_secret: - secure: gcCp/gJF8vqmnCUPKyb04H8Oz9mWmiB00U5X7iI/DGr5mxjoCG1khc6/zn6aSSqn + secure: ovfXJRorkkKUzbMXuZ4m0U6KF4icngmS+nzSljXJGSKfhI+GNXbMNa//mKYfTCXI credentials_photobucket_consumer_key: secure: oo9GD1Y8dkrli6hMfnnYsw== credentials_photobucket_consumer_secret: