BUG-1949: Fixed imgur issues, especially the delete.

This commit is contained in:
Robin 2016-04-26 23:12:45 +02:00
commit e781bdcd5c
3 changed files with 79 additions and 48 deletions

View file

@ -119,7 +119,7 @@ namespace GreenshotImgurPlugin {
if (filename != null && Config.AddFilename) {
otherParameters.Add("name", filename);
}
string responseString;
string responseString = null;
if (Config.AnonymousAccess) {
// add key, we only use the other parameters for the AnonymousAccess
//otherParameters.Add("key", IMGUR_ANONYMOUS_API_KEY);
@ -133,11 +133,17 @@ namespace GreenshotImgurPlugin {
ImageOutput.SaveToStream(surfaceToUpload, requestStream, outputSettings);
}
using (WebResponse response = webRequest.GetResponse()) {
using (StreamReader reader = new StreamReader(response.GetResponseStream(), true)) {
responseString = reader.ReadToEnd();
}
using (WebResponse response = webRequest.GetResponse())
{
LogRateLimitInfo(response);
var responseStream = response.GetResponseStream();
if (responseStream != null)
{
using (StreamReader reader = new StreamReader(responseStream, true))
{
responseString = reader.ReadToEnd();
}
}
}
} catch (Exception ex) {
LOG.Error("Upload to imgur gave an exeption: ", ex);
@ -145,20 +151,22 @@ namespace GreenshotImgurPlugin {
}
} else {
var oauth2Settings = new OAuth2Settings();
oauth2Settings.AuthUrlPattern = AuthUrlPattern;
oauth2Settings.TokenUrl = TokenUrl;
oauth2Settings.RedirectUrl = "https://imgur.com";
oauth2Settings.CloudServiceName = "Imgur";
oauth2Settings.ClientId = ImgurCredentials.CONSUMER_KEY;
oauth2Settings.ClientSecret = ImgurCredentials.CONSUMER_SECRET;
oauth2Settings.AuthorizeMode = OAuth2AuthorizeMode.EmbeddedBrowser;
oauth2Settings.BrowserSize = new Size(680, 880);
var oauth2Settings = new OAuth2Settings
{
AuthUrlPattern = AuthUrlPattern,
TokenUrl = TokenUrl,
RedirectUrl = "https://imgur.com",
CloudServiceName = "Imgur",
ClientId = ImgurCredentials.CONSUMER_KEY,
ClientSecret = ImgurCredentials.CONSUMER_SECRET,
AuthorizeMode = OAuth2AuthorizeMode.EmbeddedBrowser,
BrowserSize = new Size(680, 880),
RefreshToken = Config.RefreshToken,
AccessToken = Config.AccessToken,
AccessTokenExpires = Config.AccessTokenExpires
};
// Copy the settings from the config, which is kept in memory and on the disk
oauth2Settings.RefreshToken = Config.RefreshToken;
oauth2Settings.AccessToken = Config.AccessToken;
oauth2Settings.AccessTokenExpires = Config.AccessTokenExpires;
try
{
@ -179,6 +187,10 @@ namespace GreenshotImgurPlugin {
IniConfig.Save();
}
}
if (string.IsNullOrEmpty(responseString))
{
return null;
}
return ImgurInfo.ParseResponse(responseString);
}
@ -194,7 +206,8 @@ 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);
// Not for getting the thumbnail, in anonymous modus
//SetClientId(webRequest);
using (WebResponse response = webRequest.GetResponse()) {
LogRateLimitInfo(response);
Stream responseStream = response.GetResponseStream();
@ -212,7 +225,7 @@ namespace GreenshotImgurPlugin {
/// <param name="deleteHash"></param>
/// <returns>ImgurInfo</returns>
public static ImgurInfo RetrieveImgurInfo(string hash, string deleteHash) {
string url = Config.ImgurApiUrl + "/image/" + hash;
string url = Config.ImgurApi3Url + "/image/" + hash + ".xml";
LOG.InfoFormat("Retrieving Imgur info for {0} with url {1}", hash, url);
HttpWebRequest webRequest = NetworkHelper.CreateWebRequest(url, HTTPMethod.GET);
webRequest.ServicePoint.Expect100Continue = false;
@ -247,8 +260,8 @@ namespace GreenshotImgurPlugin {
LOG.InfoFormat("Deleting Imgur image for {0}", imgurInfo.DeleteHash);
try {
string url = Config.ImgurApiUrl + "/delete/" + imgurInfo.DeleteHash;
HttpWebRequest webRequest = NetworkHelper.CreateWebRequest(url, HTTPMethod.GET);
string url = Config.ImgurApi3Url + "/image/" + imgurInfo.DeleteHash + ".xml";
HttpWebRequest webRequest = NetworkHelper.CreateWebRequest(url, HTTPMethod.DELETE);
webRequest.ServicePoint.Expect100Continue = false;
SetClientId(webRequest);
string responseString;