From 7710db665c1d3ebd3241cb82b106ca129238ac22 Mon Sep 17 00:00:00 2001 From: RKrom Date: Sat, 3 Nov 2012 20:10:30 +0000 Subject: [PATCH] Added "Headers" support to a OAuth request, thus enabling the Picasa-Web plug-in to upload with a filename. git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2232 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4 --- GreenshotPicasaPlugin/PicasaUtils.cs | 5 ++- GreenshotPlugin/Core/OAuthHelper.cs | 48 ++++++++++++++++++++++++---- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/GreenshotPicasaPlugin/PicasaUtils.cs b/GreenshotPicasaPlugin/PicasaUtils.cs index 02491f749..ca59f1a80 100644 --- a/GreenshotPicasaPlugin/PicasaUtils.cs +++ b/GreenshotPicasaPlugin/PicasaUtils.cs @@ -18,6 +18,7 @@ * along with this program. If not, see . */ using System; +using System.Collections.Generic; using System.Drawing; using System.Xml; using Greenshot.IniFile; @@ -64,7 +65,9 @@ namespace GreenshotPicasaPlugin { IniConfig.Save(); } try { - string response = oAuth.MakeOAuthRequest(HTTPMethod.POST, "https://picasaweb.google.com/data/feed/api/user/default/albumid/default", null, null, new ImageContainer(imageToUpload, outputSettings, filename)); + IDictionary headers = new Dictionary(); + headers.Add("slug", OAuthSession.UrlEncode3986(filename)); + string response = oAuth.MakeOAuthRequest(HTTPMethod.POST, "https://picasaweb.google.com/data/feed/api/user/default/albumid/default", headers, null, null, new ImageContainer(imageToUpload, outputSettings, filename)); return ParseResponse(response); } catch (Exception ex) { LOG.Error("Upload error: ", ex); diff --git a/GreenshotPlugin/Core/OAuthHelper.cs b/GreenshotPlugin/Core/OAuthHelper.cs index 62f6480f7..64d667c61 100644 --- a/GreenshotPlugin/Core/OAuthHelper.cs +++ b/GreenshotPlugin/Core/OAuthHelper.cs @@ -295,7 +295,7 @@ namespace GreenshotPlugin.Core { parameters.Add(value); } Sign(RequestTokenMethod, RequestTokenUrl, parameters); - string response = MakeRequest(RequestTokenMethod, RequestTokenUrl, parameters, null); + string response = MakeRequest(RequestTokenMethod, RequestTokenUrl, null, parameters, null); if (response.Length > 0) { response = NetworkHelper.UrlDecode(response); LOG.DebugFormat("Request token response: {0}", response); @@ -354,7 +354,7 @@ namespace GreenshotPlugin.Core { IDictionary parameters = new Dictionary(); Sign(AccessTokenMethod, AccessTokenUrl, parameters); - string response = MakeRequest(AccessTokenMethod, AccessTokenUrl, parameters, null); + string response = MakeRequest(AccessTokenMethod, AccessTokenUrl, null, parameters, null); if (response.Length > 0) { response = NetworkHelper.UrlDecode(response); LOG.DebugFormat("Access token response: {0}", response); @@ -418,7 +418,21 @@ namespace GreenshotPlugin.Core { /// Data to post (MemoryStream) /// The web server response. public string MakeOAuthRequest(HTTPMethod method, string requestURL, IDictionary parametersToSign, IDictionary additionalParameters, IBinaryContainer postData) { - return MakeOAuthRequest(method, requestURL, requestURL, parametersToSign, additionalParameters, postData); + return MakeOAuthRequest(method, requestURL, requestURL, null, parametersToSign, additionalParameters, postData); + } + + /// + /// Submit a web request using oAuth. + /// + /// GET or POST + /// The full url, including the querystring for the signing/request + /// Header values + /// Parameters for the request, which need to be signed + /// Parameters for the request, which do not need to be signed + /// Data to post (MemoryStream) + /// The web server response. + public string MakeOAuthRequest(HTTPMethod method, string requestURL, IDictionary headers, IDictionary parametersToSign, IDictionary additionalParameters, IBinaryContainer postData) { + return MakeOAuthRequest(method, requestURL, requestURL, headers, parametersToSign, additionalParameters, postData); } /// @@ -432,6 +446,21 @@ namespace GreenshotPlugin.Core { /// Data to post (MemoryStream) /// The web server response. public string MakeOAuthRequest(HTTPMethod method, string signUrl, string requestURL, IDictionary parametersToSign, IDictionary additionalParameters, IBinaryContainer postData) { + return MakeOAuthRequest(method, signUrl, requestURL, null, parametersToSign, additionalParameters, postData); + } + + /// + /// Submit a web request using oAuth. + /// + /// GET or POST + /// The full url, including the querystring for the signing + /// The full url, including the querystring for the request + /// Headers for the request + /// Parameters for the request, which need to be signed + /// Parameters for the request, which do not need to be signed + /// Data to post (MemoryStream) + /// The web server response. + public string MakeOAuthRequest(HTTPMethod method, string signUrl, string requestURL, IDictionary headers, IDictionary parametersToSign, IDictionary additionalParameters, IBinaryContainer postData) { if (parametersToSign == null) { parametersToSign = new Dictionary(); } @@ -457,7 +486,7 @@ namespace GreenshotPlugin.Core { newParameters.Add(parameter); } } - return MakeRequest(method, requestURL, newParameters, postData); + return MakeRequest(method, requestURL, headers, newParameters, postData); } catch (WebException wEx) { lastException = wEx; if (wEx.Response != null) { @@ -487,7 +516,6 @@ namespace GreenshotPlugin.Core { throw new Exception("Not authorized"); } - /// /// OAuth sign the parameters, meaning all oauth parameters are added to the supplied dictionary. /// And additionally a signature is added. @@ -545,10 +573,11 @@ namespace GreenshotPlugin.Core { /// /// /// + /// /// /// IBinaryParameter /// Response from server - private string MakeRequest(HTTPMethod method, string requestURL, IDictionary parameters, IBinaryContainer postData) { + private string MakeRequest(HTTPMethod method, string requestURL, IDictionary headers, IDictionary parameters, IBinaryContainer postData) { if (parameters == null) { throw new ArgumentNullException("parameters"); } @@ -591,6 +620,12 @@ namespace GreenshotPlugin.Core { webRequest.Headers.Add("Authorization: OAuth " + authHeader.ToString()); } + if (headers != null) { + foreach(string key in headers.Keys) { + webRequest.Headers.Add(key, headers[key]); + } + } + if ((HTTPMethod.POST == method || HTTPMethod.PUT == method) && postData == null && requestParameters != null && requestParameters.Count > 0) { if (UseMultipartFormData) { NetworkHelper.WriteMultipartFormData(webRequest, requestParameters); @@ -628,5 +663,4 @@ namespace GreenshotPlugin.Core { return responseData; } } - }