mirror of
https://github.com/greenshot/greenshot
synced 2025-07-16 10:03:44 -07:00
BUG-1708: This change is just an improvement which writes the web-request output in case of errors. This should help to find the problem. Also cleaned the code a bit.
This commit is contained in:
parent
0e2d9cda61
commit
b9e86d4af7
4 changed files with 108 additions and 91 deletions
|
@ -30,8 +30,9 @@ namespace GreenshotPicasaPlugin {
|
||||||
/// Description of PicasaUtils.
|
/// Description of PicasaUtils.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class PicasaUtils {
|
public class PicasaUtils {
|
||||||
|
private const string GoogleAccountUri = "https://www.google.com/accounts/";
|
||||||
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(PicasaUtils));
|
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(PicasaUtils));
|
||||||
private static PicasaConfiguration config = IniConfig.GetIniSection<PicasaConfiguration>();
|
private static readonly PicasaConfiguration Config = IniConfig.GetIniSection<PicasaConfiguration>();
|
||||||
|
|
||||||
private PicasaUtils() {
|
private PicasaUtils() {
|
||||||
}
|
}
|
||||||
|
@ -39,17 +40,20 @@ namespace GreenshotPicasaPlugin {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Do the actual upload to Picasa
|
/// Do the actual upload to Picasa
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="imageData">byte[] with image data</param>
|
/// <param name="surfaceToUpload">Image to upload</param>
|
||||||
|
/// <param name="outputSettings"></param>
|
||||||
|
/// <param name="title"></param>
|
||||||
|
/// <param name="filename"></param>
|
||||||
/// <returns>PicasaResponse</returns>
|
/// <returns>PicasaResponse</returns>
|
||||||
public static string UploadToPicasa(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string title, string filename) {
|
public static string UploadToPicasa(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string title, string filename) {
|
||||||
OAuthSession oAuth = new OAuthSession(PicasaCredentials.ConsumerKey, PicasaCredentials.ConsumerSecret);
|
OAuthSession oAuth = new OAuthSession(PicasaCredentials.ConsumerKey, PicasaCredentials.ConsumerSecret);
|
||||||
oAuth.BrowserSize = new Size(1020, 590);
|
oAuth.BrowserSize = new Size(1020, 590);
|
||||||
oAuth.AccessTokenUrl = "https://www.google.com/accounts/OAuthGetAccessToken";
|
oAuth.AccessTokenUrl = GoogleAccountUri + "OAuthGetAccessToken";
|
||||||
oAuth.AuthorizeUrl = "https://www.google.com/accounts/OAuthAuthorizeToken";
|
oAuth.AuthorizeUrl = GoogleAccountUri + "OAuthAuthorizeToken";
|
||||||
oAuth.RequestTokenUrl = "https://www.google.com/accounts/OAuthGetRequestToken";
|
oAuth.RequestTokenUrl = GoogleAccountUri + "OAuthGetRequestToken";
|
||||||
oAuth.LoginTitle = "Picasa authorization";
|
oAuth.LoginTitle = "Picasa authorization";
|
||||||
oAuth.Token = config.PicasaToken;
|
oAuth.Token = Config.PicasaToken;
|
||||||
oAuth.TokenSecret = config.PicasaTokenSecret;
|
oAuth.TokenSecret = Config.PicasaTokenSecret;
|
||||||
oAuth.RequestTokenParameters.Add("scope", "https://picasaweb.google.com/data/");
|
oAuth.RequestTokenParameters.Add("scope", "https://picasaweb.google.com/data/");
|
||||||
oAuth.RequestTokenParameters.Add("xoauth_displayname", "Greenshot");
|
oAuth.RequestTokenParameters.Add("xoauth_displayname", "Greenshot");
|
||||||
if (string.IsNullOrEmpty(oAuth.Token)) {
|
if (string.IsNullOrEmpty(oAuth.Token)) {
|
||||||
|
@ -57,10 +61,10 @@ namespace GreenshotPicasaPlugin {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (!string.IsNullOrEmpty(oAuth.Token)) {
|
if (!string.IsNullOrEmpty(oAuth.Token)) {
|
||||||
config.PicasaToken = oAuth.Token;
|
Config.PicasaToken = oAuth.Token;
|
||||||
}
|
}
|
||||||
if (!string.IsNullOrEmpty(oAuth.TokenSecret)) {
|
if (!string.IsNullOrEmpty(oAuth.TokenSecret)) {
|
||||||
config.PicasaTokenSecret = oAuth.TokenSecret;
|
Config.PicasaTokenSecret = oAuth.TokenSecret;
|
||||||
}
|
}
|
||||||
IniConfig.Save();
|
IniConfig.Save();
|
||||||
}
|
}
|
||||||
|
@ -74,10 +78,10 @@ namespace GreenshotPicasaPlugin {
|
||||||
throw;
|
throw;
|
||||||
} finally {
|
} finally {
|
||||||
if (!string.IsNullOrEmpty(oAuth.Token)) {
|
if (!string.IsNullOrEmpty(oAuth.Token)) {
|
||||||
config.PicasaToken = oAuth.Token;
|
Config.PicasaToken = oAuth.Token;
|
||||||
}
|
}
|
||||||
if (!string.IsNullOrEmpty(oAuth.TokenSecret)) {
|
if (!string.IsNullOrEmpty(oAuth.TokenSecret)) {
|
||||||
config.PicasaTokenSecret = oAuth.TokenSecret;
|
Config.PicasaTokenSecret = oAuth.TokenSecret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -93,13 +97,14 @@ namespace GreenshotPicasaPlugin {
|
||||||
if(nodes.Count > 0) {
|
if(nodes.Count > 0) {
|
||||||
string url = null;
|
string url = null;
|
||||||
foreach(XmlNode node in nodes) {
|
foreach(XmlNode node in nodes) {
|
||||||
url = node.Attributes["href"].Value;
|
if (node.Attributes != null) {
|
||||||
string rel = node.Attributes["rel"].Value;
|
url = node.Attributes["href"].Value;
|
||||||
// Pictures with rel="http://schemas.google.com/photos/2007#canonical" are the direct link
|
string rel = node.Attributes["rel"].Value;
|
||||||
if (rel != null && rel.EndsWith("canonical")) {
|
// Pictures with rel="http://schemas.google.com/photos/2007#canonical" are the direct link
|
||||||
break;
|
if (rel != null && rel.EndsWith("canonical")) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,12 +49,12 @@ namespace GreenshotPlugin.Core {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Download a url response as string
|
/// Download a uri response as string
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name=url">An Uri to specify the download location</param>
|
/// <param name="uri">An Uri to specify the download location</param>
|
||||||
/// <returns>string with the file content</returns>
|
/// <returns>string with the file content</returns>
|
||||||
public static string GetAsString(Uri url) {
|
public static string GetAsString(Uri uri) {
|
||||||
HttpWebRequest webRequest = (HttpWebRequest)CreateWebRequest(url);
|
HttpWebRequest webRequest = (HttpWebRequest)CreateWebRequest(uri);
|
||||||
webRequest.Method = "GET";
|
webRequest.Method = "GET";
|
||||||
webRequest.KeepAlive = true;
|
webRequest.KeepAlive = true;
|
||||||
webRequest.Credentials = CredentialCache.DefaultCredentials;
|
webRequest.Credentials = CredentialCache.DefaultCredentials;
|
||||||
|
@ -84,7 +84,7 @@ namespace GreenshotPlugin.Core {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Download the url to Bitmap
|
/// Download the uri to Bitmap
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="baseUri"></param>
|
/// <param name="baseUri"></param>
|
||||||
/// <returns>Bitmap</returns>
|
/// <returns>Bitmap</returns>
|
||||||
|
@ -212,7 +212,7 @@ namespace GreenshotPlugin.Core {
|
||||||
/// <returns>Dictionary<string, string></returns>
|
/// <returns>Dictionary<string, string></returns>
|
||||||
public static IDictionary<string, string> ParseQueryString(string s) {
|
public static IDictionary<string, string> ParseQueryString(string s) {
|
||||||
IDictionary<string, string> parameters = new SortedDictionary<string, string>();
|
IDictionary<string, string> parameters = new SortedDictionary<string, string>();
|
||||||
// remove anything other than query string from url
|
// remove anything other than query string from uri
|
||||||
if (s.Contains("?")) {
|
if (s.Contains("?")) {
|
||||||
s = s.Substring(s.IndexOf('?') + 1);
|
s = s.Substring(s.IndexOf('?') + 1);
|
||||||
}
|
}
|
||||||
|
@ -318,18 +318,30 @@ namespace GreenshotPlugin.Core {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="webRequest">The request object.</param>
|
/// <param name="webRequest">The request object.</param>
|
||||||
/// <returns>The response data.</returns>
|
/// <returns>The response data.</returns>
|
||||||
|
/// TODO: This method should handle the StatusCode better!
|
||||||
public static string GetResponse(HttpWebRequest webRequest) {
|
public static string GetResponse(HttpWebRequest webRequest) {
|
||||||
string responseData;
|
string responseData = null;
|
||||||
try {
|
try {
|
||||||
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
|
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
|
||||||
using (StreamReader reader = new StreamReader(response.GetResponseStream(), true)) {
|
LOG.InfoFormat("Response status: {0}", response.StatusCode);
|
||||||
responseData = reader.ReadToEnd();
|
bool isHttpError = (int) response.StatusCode >= 300;
|
||||||
|
Stream responseStream = response.GetResponseStream();
|
||||||
|
if (responseStream != null) {
|
||||||
|
using (StreamReader reader = new StreamReader(responseStream, true)) {
|
||||||
|
responseData = reader.ReadToEnd();
|
||||||
|
}
|
||||||
|
if (isHttpError) {
|
||||||
|
LOG.ErrorFormat("HTTP error {0} with content: {1}", response.StatusCode, responseData);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
LOG.DebugFormat("Response status: {0}", response.StatusCode);
|
|
||||||
} catch (WebException e) {
|
} catch (WebException e) {
|
||||||
HttpWebResponse response = (HttpWebResponse)e.Response;
|
HttpWebResponse response = (HttpWebResponse)e.Response;
|
||||||
using (Stream responseStream = response.GetResponseStream()) {
|
using (Stream responseStream = response.GetResponseStream()) {
|
||||||
LOG.ErrorFormat("HTTP error {0} with content: {1}", response.StatusCode, new StreamReader(responseStream, true).ReadToEnd());
|
if (responseStream != null) {
|
||||||
|
LOG.ErrorFormat("HTTP error {0} with content: {1}", response.StatusCode, new StreamReader(responseStream, true).ReadToEnd());
|
||||||
|
} else {
|
||||||
|
LOG.ErrorFormat("HTTP error {0}", response.StatusCode);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* 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.Collections.Generic;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
|
@ -72,14 +73,14 @@ namespace GreenshotPlugin.Core {
|
||||||
|
|
||||||
private string _userAgent = "Greenshot";
|
private string _userAgent = "Greenshot";
|
||||||
private string _callbackUrl = "http://getgreenshot.org";
|
private string _callbackUrl = "http://getgreenshot.org";
|
||||||
private bool checkVerifier = true;
|
private bool _checkVerifier = true;
|
||||||
private bool useHTTPHeadersForAuthorization = true;
|
private bool _useHttpHeadersForAuthorization = true;
|
||||||
private IDictionary<string, string> accessTokenResponseParameters = null;
|
private IDictionary<string, string> _accessTokenResponseParameters;
|
||||||
private IDictionary<string, string> requestTokenResponseParameters = null;
|
private IDictionary<string, string> _requestTokenResponseParameters;
|
||||||
private IDictionary<string, object> requestTokenParameters = new Dictionary<string, object>();
|
private readonly IDictionary<string, object> _requestTokenParameters = new Dictionary<string, object>();
|
||||||
|
|
||||||
public IDictionary<string, object> RequestTokenParameters {
|
public IDictionary<string, object> RequestTokenParameters {
|
||||||
get { return requestTokenParameters; }
|
get { return _requestTokenParameters; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -87,7 +88,7 @@ namespace GreenshotPlugin.Core {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IDictionary<string, string> AccessTokenResponseParameters {
|
public IDictionary<string, string> AccessTokenResponseParameters {
|
||||||
get {
|
get {
|
||||||
return accessTokenResponseParameters;
|
return _accessTokenResponseParameters;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -95,15 +96,15 @@ namespace GreenshotPlugin.Core {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IDictionary<string, string> RequestTokenResponseParameters {
|
public IDictionary<string, string> RequestTokenResponseParameters {
|
||||||
get {
|
get {
|
||||||
return requestTokenResponseParameters;
|
return _requestTokenResponseParameters;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private string consumerKey;
|
private readonly string _consumerKey;
|
||||||
private string consumerSecret;
|
private readonly string _consumerSecret;
|
||||||
|
|
||||||
// default browser size
|
// default browser size
|
||||||
private Size _browserSize = new Size(864, 587);
|
private Size _browserSize = new Size(864, 587);
|
||||||
private string loginTitle = "Authorize Greenshot access";
|
private string _loginTitle = "Authorize Greenshot access";
|
||||||
|
|
||||||
#region PublicProperties
|
#region PublicProperties
|
||||||
public HTTPMethod RequestTokenMethod {
|
public HTTPMethod RequestTokenMethod {
|
||||||
|
@ -162,10 +163,10 @@ namespace GreenshotPlugin.Core {
|
||||||
}
|
}
|
||||||
public bool CheckVerifier {
|
public bool CheckVerifier {
|
||||||
get {
|
get {
|
||||||
return checkVerifier;
|
return _checkVerifier;
|
||||||
}
|
}
|
||||||
set {
|
set {
|
||||||
checkVerifier = value;
|
_checkVerifier = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -180,18 +181,18 @@ namespace GreenshotPlugin.Core {
|
||||||
|
|
||||||
public string LoginTitle {
|
public string LoginTitle {
|
||||||
get {
|
get {
|
||||||
return loginTitle;
|
return _loginTitle;
|
||||||
}
|
}
|
||||||
set {
|
set {
|
||||||
loginTitle = value;
|
_loginTitle = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public bool UseHTTPHeadersForAuthorization {
|
public bool UseHTTPHeadersForAuthorization {
|
||||||
get {
|
get {
|
||||||
return useHTTPHeadersForAuthorization;
|
return _useHttpHeadersForAuthorization;
|
||||||
}
|
}
|
||||||
set {
|
set {
|
||||||
useHTTPHeadersForAuthorization = value;
|
_useHttpHeadersForAuthorization = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -208,8 +209,8 @@ namespace GreenshotPlugin.Core {
|
||||||
/// <param name="consumerKey">"Public" key for the encoding. When using RSASHA1 this is the path to the private key file</param>
|
/// <param name="consumerKey">"Public" key for the encoding. When using RSASHA1 this is the path to the private key file</param>
|
||||||
/// <param name="consumerSecret">"Private" key for the encoding. when usin RSASHA1 this is the password for the private key file</param>
|
/// <param name="consumerSecret">"Private" key for the encoding. when usin RSASHA1 this is the password for the private key file</param>
|
||||||
public OAuthSession(string consumerKey, string consumerSecret) {
|
public OAuthSession(string consumerKey, string consumerSecret) {
|
||||||
this.consumerKey = consumerKey;
|
_consumerKey = consumerKey;
|
||||||
this.consumerSecret = consumerSecret;
|
_consumerSecret = consumerSecret;
|
||||||
UseMultipartFormData = true;
|
UseMultipartFormData = true;
|
||||||
RequestTokenMethod = HTTPMethod.GET;
|
RequestTokenMethod = HTTPMethod.GET;
|
||||||
AccessTokenMethod = HTTPMethod.GET;
|
AccessTokenMethod = HTTPMethod.GET;
|
||||||
|
@ -307,66 +308,63 @@ namespace GreenshotPlugin.Core {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get the request token using the consumer key and secret. Also initializes tokensecret
|
/// Get the request token using the consumer key and secret. Also initializes tokensecret
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>The request token.</returns>
|
private void GetRequestToken() {
|
||||||
private String getRequestToken() {
|
|
||||||
string ret = null;
|
|
||||||
IDictionary<string, object> parameters = new Dictionary<string, object>();
|
IDictionary<string, object> parameters = new Dictionary<string, object>();
|
||||||
foreach(var value in requestTokenParameters) {
|
foreach(var value in _requestTokenParameters) {
|
||||||
parameters.Add(value);
|
parameters.Add(value);
|
||||||
}
|
}
|
||||||
Sign(RequestTokenMethod, RequestTokenUrl, parameters);
|
Sign(RequestTokenMethod, RequestTokenUrl, parameters);
|
||||||
string response = MakeRequest(RequestTokenMethod, RequestTokenUrl, null, parameters, null);
|
string response = MakeRequest(RequestTokenMethod, RequestTokenUrl, null, parameters, null);
|
||||||
if (response != null && response.Length > 0) {
|
if (!string.IsNullOrEmpty(response)) {
|
||||||
response = NetworkHelper.UrlDecode(response);
|
response = NetworkHelper.UrlDecode(response);
|
||||||
LOG.DebugFormat("Request token response: {0}", response);
|
LOG.DebugFormat("Request token response: {0}", response);
|
||||||
requestTokenResponseParameters = NetworkHelper.ParseQueryString(response);
|
_requestTokenResponseParameters = NetworkHelper.ParseQueryString(response);
|
||||||
if (requestTokenResponseParameters.ContainsKey(OAUTH_TOKEN_KEY)) {
|
string value;
|
||||||
Token = requestTokenResponseParameters[OAUTH_TOKEN_KEY];
|
if (_requestTokenResponseParameters.TryGetValue(OAUTH_TOKEN_KEY, out value)) {
|
||||||
TokenSecret = requestTokenResponseParameters[OAUTH_TOKEN_SECRET_KEY];
|
Token = value;
|
||||||
ret = Token;
|
TokenSecret = _requestTokenResponseParameters[OAUTH_TOKEN_SECRET_KEY];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Authorize the token by showing the dialog
|
/// Authorize the token by showing the dialog
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>The request token.</returns>
|
/// <returns>The request token.</returns>
|
||||||
private String getAuthorizeToken() {
|
private String GetAuthorizeToken() {
|
||||||
if (string.IsNullOrEmpty(Token)) {
|
if (string.IsNullOrEmpty(Token)) {
|
||||||
Exception e = new Exception("The request token is not set");
|
Exception e = new Exception("The request token is not set");
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
LOG.DebugFormat("Opening AuthorizationLink: {0}", authorizationLink);
|
LOG.DebugFormat("Opening AuthorizationLink: {0}", AuthorizationLink);
|
||||||
OAuthLoginForm oAuthLoginForm = new OAuthLoginForm(LoginTitle, BrowserSize, authorizationLink, CallbackUrl);
|
OAuthLoginForm oAuthLoginForm = new OAuthLoginForm(LoginTitle, BrowserSize, AuthorizationLink, CallbackUrl);
|
||||||
oAuthLoginForm.ShowDialog();
|
oAuthLoginForm.ShowDialog();
|
||||||
if (oAuthLoginForm.isOk) {
|
if (oAuthLoginForm.isOk) {
|
||||||
if (oAuthLoginForm.CallbackParameters != null) {
|
if (oAuthLoginForm.CallbackParameters != null) {
|
||||||
if (oAuthLoginForm.CallbackParameters.ContainsKey(OAUTH_TOKEN_KEY)) {
|
string tokenValue;
|
||||||
Token = oAuthLoginForm.CallbackParameters[OAUTH_TOKEN_KEY];
|
if (oAuthLoginForm.CallbackParameters.TryGetValue(OAUTH_TOKEN_KEY, out tokenValue)) {
|
||||||
|
Token = tokenValue;
|
||||||
}
|
}
|
||||||
if (oAuthLoginForm.CallbackParameters.ContainsKey(OAUTH_VERIFIER_KEY)) {
|
string verifierValue;
|
||||||
Verifier = oAuthLoginForm.CallbackParameters[OAUTH_VERIFIER_KEY];
|
if (oAuthLoginForm.CallbackParameters.TryGetValue(OAUTH_VERIFIER_KEY, out verifierValue)) {
|
||||||
|
Verifier = verifierValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (CheckVerifier) {
|
if (CheckVerifier) {
|
||||||
if (!string.IsNullOrEmpty(Verifier)) {
|
if (!string.IsNullOrEmpty(Verifier)) {
|
||||||
return Token;
|
return Token;
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
} else {
|
return null;
|
||||||
return Token;
|
|
||||||
}
|
}
|
||||||
|
return Token;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get the access token
|
/// Get the access token
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>The access token.</returns>
|
/// <returns>The access token.</returns>
|
||||||
private String getAccessToken() {
|
private String GetAccessToken() {
|
||||||
if (string.IsNullOrEmpty(Token) || (CheckVerifier && string.IsNullOrEmpty(Verifier))) {
|
if (string.IsNullOrEmpty(Token) || (CheckVerifier && string.IsNullOrEmpty(Verifier))) {
|
||||||
Exception e = new Exception("The request token and verifier were not set");
|
Exception e = new Exception("The request token and verifier were not set");
|
||||||
throw e;
|
throw e;
|
||||||
|
@ -375,15 +373,17 @@ 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, null, parameters, null);
|
string response = MakeRequest(AccessTokenMethod, AccessTokenUrl, null, parameters, null);
|
||||||
if (response != null && response.Length > 0) {
|
if (!string.IsNullOrEmpty(response)) {
|
||||||
response = NetworkHelper.UrlDecode(response);
|
response = NetworkHelper.UrlDecode(response);
|
||||||
LOG.DebugFormat("Access token response: {0}", response);
|
LOG.DebugFormat("Access token response: {0}", response);
|
||||||
accessTokenResponseParameters = NetworkHelper.ParseQueryString(response);
|
_accessTokenResponseParameters = NetworkHelper.ParseQueryString(response);
|
||||||
if (accessTokenResponseParameters.ContainsKey(OAUTH_TOKEN_KEY) && accessTokenResponseParameters[OAUTH_TOKEN_KEY] != null) {
|
string tokenValue;
|
||||||
Token = accessTokenResponseParameters[OAUTH_TOKEN_KEY];
|
if (_accessTokenResponseParameters.TryGetValue(OAUTH_TOKEN_KEY, out tokenValue) && tokenValue != null) {
|
||||||
|
Token = tokenValue;
|
||||||
}
|
}
|
||||||
if (accessTokenResponseParameters.ContainsKey(OAUTH_TOKEN_SECRET_KEY) && accessTokenResponseParameters[OAUTH_TOKEN_SECRET_KEY] != null) {
|
string secretValue;
|
||||||
TokenSecret = accessTokenResponseParameters[OAUTH_TOKEN_SECRET_KEY];
|
if (_accessTokenResponseParameters.TryGetValue(OAUTH_TOKEN_SECRET_KEY, out secretValue) && secretValue != null) {
|
||||||
|
TokenSecret = secretValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -400,18 +400,18 @@ namespace GreenshotPlugin.Core {
|
||||||
Verifier = null;
|
Verifier = null;
|
||||||
LOG.Debug("Creating Token");
|
LOG.Debug("Creating Token");
|
||||||
try {
|
try {
|
||||||
getRequestToken();
|
GetRequestToken();
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
LOG.Error(ex);
|
LOG.Error(ex);
|
||||||
throw new NotSupportedException("Service is not available: " + ex.Message);
|
throw new NotSupportedException("Service is not available: " + ex.Message);
|
||||||
}
|
}
|
||||||
if (string.IsNullOrEmpty(getAuthorizeToken())) {
|
if (string.IsNullOrEmpty(GetAuthorizeToken())) {
|
||||||
LOG.Debug("User didn't authenticate!");
|
LOG.Debug("User didn't authenticate!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Thread.Sleep(1000);
|
Thread.Sleep(1000);
|
||||||
return getAccessToken() != null;
|
return GetAccessToken() != null;
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
LOG.Error(ex);
|
LOG.Error(ex);
|
||||||
throw;
|
throw;
|
||||||
|
@ -422,7 +422,7 @@ namespace GreenshotPlugin.Core {
|
||||||
/// Get the link to the authorization page for this application.
|
/// Get the link to the authorization page for this application.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>The url with a valid request token, or a null string.</returns>
|
/// <returns>The url with a valid request token, or a null string.</returns>
|
||||||
private string authorizationLink {
|
private string AuthorizationLink {
|
||||||
get {
|
get {
|
||||||
return AuthorizeUrl + "?" + OAUTH_TOKEN_KEY + "=" + Token + "&" + OAUTH_CALLBACK_KEY + "=" + UrlEncode3986(CallbackUrl);
|
return AuthorizeUrl + "?" + OAUTH_TOKEN_KEY + "=" + Token + "&" + OAUTH_CALLBACK_KEY + "=" + UrlEncode3986(CallbackUrl);
|
||||||
}
|
}
|
||||||
|
@ -578,8 +578,8 @@ namespace GreenshotPlugin.Core {
|
||||||
parameters.Add(OAUTH_SIGNATURE_METHOD_KEY, HMACSHA1SignatureType);
|
parameters.Add(OAUTH_SIGNATURE_METHOD_KEY, HMACSHA1SignatureType);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
parameters.Add(OAUTH_CONSUMER_KEY_KEY, consumerKey);
|
parameters.Add(OAUTH_CONSUMER_KEY_KEY, _consumerKey);
|
||||||
if (CallbackUrl != null && RequestTokenUrl != null && requestURL.ToString().StartsWith(RequestTokenUrl)) {
|
if (CallbackUrl != null && RequestTokenUrl != null && requestURL.StartsWith(RequestTokenUrl)) {
|
||||||
parameters.Add(OAUTH_CALLBACK_KEY, CallbackUrl);
|
parameters.Add(OAUTH_CALLBACK_KEY, CallbackUrl);
|
||||||
}
|
}
|
||||||
if (!string.IsNullOrEmpty(Verifier)) {
|
if (!string.IsNullOrEmpty(Verifier)) {
|
||||||
|
@ -590,13 +590,13 @@ namespace GreenshotPlugin.Core {
|
||||||
}
|
}
|
||||||
signatureBase.Append(UrlEncode3986(GenerateNormalizedParametersString(parameters)));
|
signatureBase.Append(UrlEncode3986(GenerateNormalizedParametersString(parameters)));
|
||||||
LOG.DebugFormat("Signature base: {0}", signatureBase);
|
LOG.DebugFormat("Signature base: {0}", signatureBase);
|
||||||
string key = string.Format(CultureInfo.InvariantCulture, "{0}&{1}", UrlEncode3986(consumerSecret), string.IsNullOrEmpty(TokenSecret) ? string.Empty : UrlEncode3986(TokenSecret));
|
string key = string.Format(CultureInfo.InvariantCulture, "{0}&{1}", UrlEncode3986(_consumerSecret), string.IsNullOrEmpty(TokenSecret) ? string.Empty : UrlEncode3986(TokenSecret));
|
||||||
switch (SignatureType) {
|
switch (SignatureType) {
|
||||||
case OAuthSignatureTypes.RSASHA1:
|
case OAuthSignatureTypes.RSASHA1:
|
||||||
// Code comes from here: http://www.dotnetfunda.com/articles/article1932-rest-service-call-using-oauth-10-authorization-with-rsa-sha1.aspx
|
// Code comes from here: http://www.dotnetfunda.com/articles/article1932-rest-service-call-using-oauth-10-authorization-with-rsa-sha1.aspx
|
||||||
// Read the .P12 file to read Private/Public key Certificate
|
// Read the .P12 file to read Private/Public key Certificate
|
||||||
string certFilePath = consumerKey; // The .P12 certificate file path Example: "C:/mycertificate/MCOpenAPI.p12
|
string certFilePath = _consumerKey; // The .P12 certificate file path Example: "C:/mycertificate/MCOpenAPI.p12
|
||||||
string password = consumerSecret; // password to read certificate .p12 file
|
string password = _consumerSecret; // password to read certificate .p12 file
|
||||||
// Read the Certification from .P12 file.
|
// Read the Certification from .P12 file.
|
||||||
X509Certificate2 cert = new X509Certificate2(certFilePath.ToString(), password);
|
X509Certificate2 cert = new X509Certificate2(certFilePath.ToString(), password);
|
||||||
// Retrieve the Private key from Certificate.
|
// Retrieve the Private key from Certificate.
|
||||||
|
@ -644,7 +644,7 @@ namespace GreenshotPlugin.Core {
|
||||||
if (parameters == null) {
|
if (parameters == null) {
|
||||||
throw new ArgumentNullException("parameters");
|
throw new ArgumentNullException("parameters");
|
||||||
}
|
}
|
||||||
IDictionary<string, object> requestParameters = null;
|
IDictionary<string, object> requestParameters;
|
||||||
// Add oAuth values as HTTP headers, if this is allowed
|
// Add oAuth values as HTTP headers, if this is allowed
|
||||||
StringBuilder authHeader = null;
|
StringBuilder authHeader = null;
|
||||||
if (UseHTTPHeadersForAuthorization) {
|
if (UseHTTPHeadersForAuthorization) {
|
||||||
|
@ -679,8 +679,8 @@ namespace GreenshotPlugin.Core {
|
||||||
webRequest.Timeout = 20000;
|
webRequest.Timeout = 20000;
|
||||||
|
|
||||||
if (UseHTTPHeadersForAuthorization && authHeader != null) {
|
if (UseHTTPHeadersForAuthorization && authHeader != null) {
|
||||||
LOG.DebugFormat("Authorization: OAuth {0}", authHeader.ToString());
|
LOG.DebugFormat("Authorization: OAuth {0}", authHeader);
|
||||||
webRequest.Headers.Add("Authorization: OAuth " + authHeader.ToString());
|
webRequest.Headers.Add("Authorization: OAuth " + authHeader);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (headers != null) {
|
if (headers != null) {
|
||||||
|
@ -689,7 +689,7 @@ namespace GreenshotPlugin.Core {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((HTTPMethod.POST == method || HTTPMethod.PUT == method) && postData == null && requestParameters != null && requestParameters.Count > 0) {
|
if ((HTTPMethod.POST == method || HTTPMethod.PUT == method) && postData == null && requestParameters.Count > 0) {
|
||||||
if (UseMultipartFormData) {
|
if (UseMultipartFormData) {
|
||||||
NetworkHelper.WriteMultipartFormData(webRequest, requestParameters);
|
NetworkHelper.WriteMultipartFormData(webRequest, requestParameters);
|
||||||
} else {
|
} else {
|
||||||
|
@ -718,7 +718,7 @@ namespace GreenshotPlugin.Core {
|
||||||
webRequest.ContentLength = 0;
|
webRequest.ContentLength = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
string responseData = null;
|
string responseData;
|
||||||
try {
|
try {
|
||||||
responseData = NetworkHelper.GetResponse(webRequest);
|
responseData = NetworkHelper.GetResponse(webRequest);
|
||||||
LOG.DebugFormat("Response: {0}", responseData);
|
LOG.DebugFormat("Response: {0}", responseData);
|
||||||
|
|
|
@ -209,7 +209,7 @@ namespace Greenshot.IniFile {
|
||||||
}
|
}
|
||||||
if (iniFilePath == null) {
|
if (iniFilePath == null) {
|
||||||
// check if file is in the same location as started from, if this is the case
|
// check if file is in the same location as started from, if this is the case
|
||||||
// we will use this file instead of the Applicationdate folder
|
// we will use this file instead of the ApplicationData folder
|
||||||
// Done for Feature Request #2741508
|
// Done for Feature Request #2741508
|
||||||
iniFilePath = Path.Combine(applicationStartupPath, configFilename);
|
iniFilePath = Path.Combine(applicationStartupPath, configFilename);
|
||||||
if (!File.Exists(iniFilePath)) {
|
if (!File.Exists(iniFilePath)) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue