diff --git a/GreenshotPhotobucketPlugin/PhotobucketDestination.cs b/GreenshotPhotobucketPlugin/PhotobucketDestination.cs index 974e9c67f..0d2bd19df 100644 --- a/GreenshotPhotobucketPlugin/PhotobucketDestination.cs +++ b/GreenshotPhotobucketPlugin/PhotobucketDestination.cs @@ -75,12 +75,16 @@ namespace GreenshotPhotobucketPlugin { } public override IEnumerable DynamicDestinations() { - List albums = PhotobucketUtils.RetrievePhotobucketAlbums(); + List albums = null; + try { + albums = PhotobucketUtils.RetrievePhotobucketAlbums(); + } catch { + } if (albums == null || albums.Count == 0) { yield break; } - foreach(string album in albums) { + foreach (string album in albums) { yield return new PhotobucketDestination(plugin, album); } } diff --git a/GreenshotPhotobucketPlugin/PhotobucketUtils.cs b/GreenshotPhotobucketPlugin/PhotobucketUtils.cs index 8d1602c7d..53098dbcf 100644 --- a/GreenshotPhotobucketPlugin/PhotobucketUtils.cs +++ b/GreenshotPhotobucketPlugin/PhotobucketUtils.cs @@ -47,8 +47,10 @@ namespace GreenshotPhotobucketPlugin { albumPath = "!"; } - OAuthSession oAuth = createSession(); - + OAuthSession oAuth = createSession(true); + if (oAuth == null) { + return null; + } IDictionary signedParameters = new Dictionary(); // add album if (albumPath == null) { @@ -83,6 +85,9 @@ namespace GreenshotPhotobucketPlugin { config.TokenSecret = oAuth.TokenSecret; } } + if (responseString == null) { + return null; + } LOG.Info(responseString); PhotobucketInfo PhotobucketInfo = PhotobucketInfo.FromUploadResponse(responseString); LOG.Debug("Upload to Photobucket was finished"); @@ -93,8 +98,9 @@ namespace GreenshotPhotobucketPlugin { /// Helper method to create an OAuth session object for contacting the Photobucket API /// /// OAuthSession - private static OAuthSession createSession() { + 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"; @@ -108,6 +114,9 @@ namespace GreenshotPhotobucketPlugin { 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; } @@ -140,8 +149,10 @@ namespace GreenshotPhotobucketPlugin { } string responseString; - OAuthSession oAuth = createSession(); - + OAuthSession oAuth = createSession(false); + if (oAuth == null) { + return null; + } IDictionary signedParameters = new Dictionary(); try { string apiUrl = string.Format("http://api.photobucket.com/album/{0}", config.Username); @@ -157,6 +168,9 @@ namespace GreenshotPhotobucketPlugin { config.TokenSecret = oAuth.TokenSecret; } } + if (responseString == null) { + return null; + } try { XmlDocument doc = new XmlDocument(); doc.LoadXml(responseString); diff --git a/GreenshotPlugin/Core/OAuthHelper.cs b/GreenshotPlugin/Core/OAuthHelper.cs index e8ca8a930..609debe42 100644 --- a/GreenshotPlugin/Core/OAuthHelper.cs +++ b/GreenshotPlugin/Core/OAuthHelper.cs @@ -191,6 +191,11 @@ namespace GreenshotPlugin.Core { useHTTPHeadersForAuthorization = value; } } + + public bool AutoLogin { + get; + set; + } #endregion @@ -206,6 +211,7 @@ namespace GreenshotPlugin.Core { this.RequestTokenMethod = HTTPMethod.GET; this.AccessTokenMethod = HTTPMethod.GET; this.SignatureType = OAuthSignatureTypes.HMACSHA1; + this.AutoLogin = true; } /// @@ -307,7 +313,7 @@ namespace GreenshotPlugin.Core { } Sign(RequestTokenMethod, RequestTokenUrl, parameters); string response = MakeRequest(RequestTokenMethod, RequestTokenUrl, null, parameters, null); - if (response.Length > 0) { + if (response != null && response.Length > 0) { response = NetworkHelper.UrlDecode(response); LOG.DebugFormat("Request token response: {0}", response); requestTokenResponseParameters = NetworkHelper.ParseQueryString(response); @@ -317,7 +323,7 @@ namespace GreenshotPlugin.Core { ret = this.Token; } } - return ret; + return ret; } /// @@ -366,7 +372,7 @@ namespace GreenshotPlugin.Core { IDictionary parameters = new Dictionary(); Sign(AccessTokenMethod, AccessTokenUrl, parameters); string response = MakeRequest(AccessTokenMethod, AccessTokenUrl, null, parameters, null); - if (response.Length > 0) { + if (response != null && response.Length > 0) { response = NetworkHelper.UrlDecode(response); LOG.DebugFormat("Access token response: {0}", response); accessTokenResponseParameters = NetworkHelper.ParseQueryString(response); @@ -378,7 +384,7 @@ namespace GreenshotPlugin.Core { } } - return Token; + return Token; } /// @@ -480,7 +486,7 @@ namespace GreenshotPlugin.Core { while (retries-- > 0) { // If we are not trying to get a Authorization or Accestoken, and we don't have a token, create one if (string.IsNullOrEmpty(Token)) { - if (!Authorize()) { + if (!AutoLogin || !Authorize()) { throw new Exception("Not authorized"); } } @@ -709,10 +715,16 @@ namespace GreenshotPlugin.Core { webRequest.ContentLength = 0; } - string responseData = NetworkHelper.GetResponse(webRequest); - LOG.DebugFormat("Response: {0}", responseData); - - webRequest = null; + string responseData = null; + try { + responseData = NetworkHelper.GetResponse(webRequest); + LOG.DebugFormat("Response: {0}", responseData); + } catch (Exception ex) { + LOG.Error("Couldn't retrieve response: ", ex); + throw; + } finally { + webRequest = null; + } return responseData; }