mirror of
https://github.com/greenshot/greenshot
synced 2025-08-19 21:13:23 -07:00
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
This commit is contained in:
parent
8191c84c0e
commit
7710db665c
2 changed files with 45 additions and 8 deletions
|
@ -18,6 +18,7 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Xml;
|
using System.Xml;
|
||||||
using Greenshot.IniFile;
|
using Greenshot.IniFile;
|
||||||
|
@ -64,7 +65,9 @@ namespace GreenshotPicasaPlugin {
|
||||||
IniConfig.Save();
|
IniConfig.Save();
|
||||||
}
|
}
|
||||||
try {
|
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<string, string> headers = new Dictionary<string, string>();
|
||||||
|
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);
|
return ParseResponse(response);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
LOG.Error("Upload error: ", ex);
|
LOG.Error("Upload error: ", ex);
|
||||||
|
|
|
@ -295,7 +295,7 @@ namespace GreenshotPlugin.Core {
|
||||||
parameters.Add(value);
|
parameters.Add(value);
|
||||||
}
|
}
|
||||||
Sign(RequestTokenMethod, RequestTokenUrl, parameters);
|
Sign(RequestTokenMethod, RequestTokenUrl, parameters);
|
||||||
string response = MakeRequest(RequestTokenMethod, RequestTokenUrl, parameters, null);
|
string response = MakeRequest(RequestTokenMethod, RequestTokenUrl, null, parameters, null);
|
||||||
if (response.Length > 0) {
|
if (response.Length > 0) {
|
||||||
response = NetworkHelper.UrlDecode(response);
|
response = NetworkHelper.UrlDecode(response);
|
||||||
LOG.DebugFormat("Request token response: {0}", response);
|
LOG.DebugFormat("Request token response: {0}", response);
|
||||||
|
@ -354,7 +354,7 @@ namespace GreenshotPlugin.Core {
|
||||||
|
|
||||||
IDictionary<string, object> parameters = new Dictionary<string, object>();
|
IDictionary<string, object> parameters = new Dictionary<string, object>();
|
||||||
Sign(AccessTokenMethod, AccessTokenUrl, parameters);
|
Sign(AccessTokenMethod, AccessTokenUrl, parameters);
|
||||||
string response = MakeRequest(AccessTokenMethod, AccessTokenUrl, parameters, null);
|
string response = MakeRequest(AccessTokenMethod, AccessTokenUrl, null, parameters, null);
|
||||||
if (response.Length > 0) {
|
if (response.Length > 0) {
|
||||||
response = NetworkHelper.UrlDecode(response);
|
response = NetworkHelper.UrlDecode(response);
|
||||||
LOG.DebugFormat("Access token response: {0}", response);
|
LOG.DebugFormat("Access token response: {0}", response);
|
||||||
|
@ -418,7 +418,21 @@ namespace GreenshotPlugin.Core {
|
||||||
/// <param name="postData">Data to post (MemoryStream)</param>
|
/// <param name="postData">Data to post (MemoryStream)</param>
|
||||||
/// <returns>The web server response.</returns>
|
/// <returns>The web server response.</returns>
|
||||||
public string MakeOAuthRequest(HTTPMethod method, string requestURL, IDictionary<string, object> parametersToSign, IDictionary<string, object> additionalParameters, IBinaryContainer postData) {
|
public string MakeOAuthRequest(HTTPMethod method, string requestURL, IDictionary<string, object> parametersToSign, IDictionary<string, object> additionalParameters, IBinaryContainer postData) {
|
||||||
return MakeOAuthRequest(method, requestURL, requestURL, parametersToSign, additionalParameters, postData);
|
return MakeOAuthRequest(method, requestURL, requestURL, null, parametersToSign, additionalParameters, postData);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Submit a web request using oAuth.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="method">GET or POST</param>
|
||||||
|
/// <param name="requestURL">The full url, including the querystring for the signing/request</param>
|
||||||
|
/// <param name="headers">Header values</param>
|
||||||
|
/// <param name="parametersToSign">Parameters for the request, which need to be signed</param>
|
||||||
|
/// <param name="additionalParameters">Parameters for the request, which do not need to be signed</param>
|
||||||
|
/// <param name="postData">Data to post (MemoryStream)</param>
|
||||||
|
/// <returns>The web server response.</returns>
|
||||||
|
public string MakeOAuthRequest(HTTPMethod method, string requestURL, IDictionary<string, string> headers, IDictionary<string, object> parametersToSign, IDictionary<string, object> additionalParameters, IBinaryContainer postData) {
|
||||||
|
return MakeOAuthRequest(method, requestURL, requestURL, headers, parametersToSign, additionalParameters, postData);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -432,6 +446,21 @@ namespace GreenshotPlugin.Core {
|
||||||
/// <param name="postData">Data to post (MemoryStream)</param>
|
/// <param name="postData">Data to post (MemoryStream)</param>
|
||||||
/// <returns>The web server response.</returns>
|
/// <returns>The web server response.</returns>
|
||||||
public string MakeOAuthRequest(HTTPMethod method, string signUrl, string requestURL, IDictionary<string, object> parametersToSign, IDictionary<string, object> additionalParameters, IBinaryContainer postData) {
|
public string MakeOAuthRequest(HTTPMethod method, string signUrl, string requestURL, IDictionary<string, object> parametersToSign, IDictionary<string, object> additionalParameters, IBinaryContainer postData) {
|
||||||
|
return MakeOAuthRequest(method, signUrl, requestURL, null, parametersToSign, additionalParameters, postData);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Submit a web request using oAuth.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="method">GET or POST</param>
|
||||||
|
/// <param name="signUrl">The full url, including the querystring for the signing</param>
|
||||||
|
/// <param name="requestURL">The full url, including the querystring for the request</param>
|
||||||
|
/// <param name="headers">Headers for the request</param>
|
||||||
|
/// <param name="parametersToSign">Parameters for the request, which need to be signed</param>
|
||||||
|
/// <param name="additionalParameters">Parameters for the request, which do not need to be signed</param>
|
||||||
|
/// <param name="postData">Data to post (MemoryStream)</param>
|
||||||
|
/// <returns>The web server response.</returns>
|
||||||
|
public string MakeOAuthRequest(HTTPMethod method, string signUrl, string requestURL, IDictionary<string, string> headers, IDictionary<string, object> parametersToSign, IDictionary<string, object> additionalParameters, IBinaryContainer postData) {
|
||||||
if (parametersToSign == null) {
|
if (parametersToSign == null) {
|
||||||
parametersToSign = new Dictionary<string, object>();
|
parametersToSign = new Dictionary<string, object>();
|
||||||
}
|
}
|
||||||
|
@ -457,7 +486,7 @@ namespace GreenshotPlugin.Core {
|
||||||
newParameters.Add(parameter);
|
newParameters.Add(parameter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return MakeRequest(method, requestURL, newParameters, postData);
|
return MakeRequest(method, requestURL, headers, newParameters, postData);
|
||||||
} catch (WebException wEx) {
|
} catch (WebException wEx) {
|
||||||
lastException = wEx;
|
lastException = wEx;
|
||||||
if (wEx.Response != null) {
|
if (wEx.Response != null) {
|
||||||
|
@ -487,7 +516,6 @@ namespace GreenshotPlugin.Core {
|
||||||
throw new Exception("Not authorized");
|
throw new Exception("Not authorized");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// OAuth sign the parameters, meaning all oauth parameters are added to the supplied dictionary.
|
/// OAuth sign the parameters, meaning all oauth parameters are added to the supplied dictionary.
|
||||||
/// And additionally a signature is added.
|
/// And additionally a signature is added.
|
||||||
|
@ -545,10 +573,11 @@ namespace GreenshotPlugin.Core {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="method"></param>
|
/// <param name="method"></param>
|
||||||
/// <param name="requestURL"></param>
|
/// <param name="requestURL"></param>
|
||||||
|
/// <param name="headers"></param>
|
||||||
/// <param name="parameters"></param>
|
/// <param name="parameters"></param>
|
||||||
/// <param name="postData">IBinaryParameter</param>
|
/// <param name="postData">IBinaryParameter</param>
|
||||||
/// <returns>Response from server</returns>
|
/// <returns>Response from server</returns>
|
||||||
private string MakeRequest(HTTPMethod method, string requestURL, IDictionary<string, object> parameters, IBinaryContainer postData) {
|
private string MakeRequest(HTTPMethod method, string requestURL, IDictionary<string, string> headers, IDictionary<string, object> parameters, IBinaryContainer postData) {
|
||||||
if (parameters == null) {
|
if (parameters == null) {
|
||||||
throw new ArgumentNullException("parameters");
|
throw new ArgumentNullException("parameters");
|
||||||
}
|
}
|
||||||
|
@ -591,6 +620,12 @@ namespace GreenshotPlugin.Core {
|
||||||
webRequest.Headers.Add("Authorization: OAuth " + authHeader.ToString());
|
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 ((HTTPMethod.POST == method || HTTPMethod.PUT == method) && postData == null && requestParameters != null && requestParameters.Count > 0) {
|
||||||
if (UseMultipartFormData) {
|
if (UseMultipartFormData) {
|
||||||
NetworkHelper.WriteMultipartFormData(webRequest, requestParameters);
|
NetworkHelper.WriteMultipartFormData(webRequest, requestParameters);
|
||||||
|
@ -628,5 +663,4 @@ namespace GreenshotPlugin.Core {
|
||||||
return responseData;
|
return responseData;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue