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
This commit is contained in:
RKrom 2012-09-09 14:33:33 +00:00
commit dcb66c5007
4 changed files with 89 additions and 27 deletions

View file

@ -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));
}
}
}