mirror of
https://github.com/greenshot/greenshot
synced 2025-08-14 02:37:03 -07:00
Moved some code around, for better re-usage. Also added some debug information, and wrote some comments. [skip ci]
This commit is contained in:
parent
6f8434d46f
commit
ea8c27449f
4 changed files with 196 additions and 160 deletions
|
@ -17,14 +17,13 @@
|
|||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Xml;
|
||||
|
||||
using Greenshot.IniFile;
|
||||
using Greenshot.Plugin;
|
||||
using GreenshotPlugin.Core;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Xml;
|
||||
|
||||
namespace GreenshotPicasaPlugin {
|
||||
/// <summary>
|
||||
|
@ -38,98 +37,6 @@ namespace GreenshotPicasaPlugin {
|
|||
private const string TokenUrl = "https://www.googleapis.com/oauth2/v3/token";
|
||||
private const string UploadUrl = "https://picasaweb.google.com/data/feed/api/user/{0}/albumid/{1}";
|
||||
|
||||
/// <summary>
|
||||
/// Authenticate by using the LocalServerCodeReceiver
|
||||
/// If this works, generate a token
|
||||
/// </summary>
|
||||
/// <param name="settings"></param>
|
||||
private static void Authenticate(OAuth2Settings settings) {
|
||||
var codeReceiver = new LocalServerCodeReceiver();
|
||||
IDictionary<string, string> result = codeReceiver.ReceiveCode(settings);
|
||||
|
||||
string code;
|
||||
if (result.TryGetValue("code", out code)) {
|
||||
GenerateToken(code, settings);
|
||||
}
|
||||
string error;
|
||||
if (result.TryGetValue("error", out error)) {
|
||||
if ("access_denied" == error) {
|
||||
throw new UnauthorizedAccessException("Access denied");
|
||||
} else {
|
||||
throw new Exception(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Upload parameters by post
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="parameters"></param>
|
||||
/// <returns>response</returns>
|
||||
public static string HttpPost(string url, IDictionary<string, object> parameters, OAuth2Settings settings) {
|
||||
var webRequest = (HttpWebRequest)NetworkHelper.CreateWebRequest(url);
|
||||
webRequest.Method = "POST";
|
||||
webRequest.KeepAlive = true;
|
||||
webRequest.Credentials = CredentialCache.DefaultCredentials;
|
||||
|
||||
if (!string.IsNullOrEmpty(settings.AccessToken)) {
|
||||
webRequest.Headers.Add("Authorization", "Bearer " + settings.AccessToken);
|
||||
}
|
||||
return NetworkHelper.UploadFormUrlEncoded(webRequest, parameters);
|
||||
}
|
||||
|
||||
private static void GenerateToken(string code, OAuth2Settings settings) {
|
||||
// Use the returned code to get a refresh code
|
||||
IDictionary<string, object> data = new Dictionary<string, object>();
|
||||
data.Add("code", code);
|
||||
data.Add("client_id", settings.ClientId);
|
||||
data.Add("redirect_uri", settings.RedirectUrl);
|
||||
data.Add("client_secret", settings.ClientSecret);
|
||||
data.Add("grant_type", "authorization_code");
|
||||
|
||||
var accessTokenJsonResult = HttpPost(settings.FormattedTokenUrl, data, settings);
|
||||
IDictionary<string, object> refreshTokenResult = JSONHelper.JsonDecode(accessTokenJsonResult);
|
||||
// gives as described here: https://developers.google.com/identity/protocols/OAuth2InstalledApp
|
||||
// "access_token":"1/fFAGRNJru1FTz70BzhT3Zg",
|
||||
// "expires_in":3920,
|
||||
// "token_type":"Bearer",
|
||||
// "refresh_token":"1/xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI"
|
||||
settings.AccessToken = (string)refreshTokenResult["access_token"] as string;
|
||||
settings.RefreshToken = (string)refreshTokenResult["refresh_token"] as string;
|
||||
|
||||
object seconds = refreshTokenResult["expires_in"];
|
||||
if (seconds != null) {
|
||||
settings.AccessTokenExpires = DateTimeOffset.Now.AddSeconds((double)seconds);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Go out and retrieve a new access token via refresh-token with the TokenUrl in the settings
|
||||
/// Will upate the access token, refresh token, expire date
|
||||
/// </summary>
|
||||
/// <param name="settings"></param>
|
||||
private static void GenerateAccessToken(OAuth2Settings settings) {
|
||||
IDictionary<string, object> data = new Dictionary<string, object>();
|
||||
data.Add("refresh_token", settings.RefreshToken);
|
||||
data.Add("client_id", settings.ClientId);
|
||||
data.Add("client_secret", settings.ClientSecret);
|
||||
data.Add("grant_type", "refresh_token");
|
||||
|
||||
var accessTokenJsonResult = HttpPost(settings.FormattedTokenUrl, data, settings);
|
||||
// gives as described here: https://developers.google.com/identity/protocols/OAuth2InstalledApp
|
||||
// "access_token":"1/fFAGRNJru1FTz70BzhT3Zg",
|
||||
// "expires_in":3920,
|
||||
// "token_type":"Bearer",
|
||||
|
||||
IDictionary<string, object> accessTokenResult = JSONHelper.JsonDecode(accessTokenJsonResult);
|
||||
settings.AccessToken = (string)accessTokenResult["access_token"] as string;
|
||||
object seconds = accessTokenResult["expires_in"];
|
||||
if (seconds != null) {
|
||||
settings.AccessTokenExpires = DateTimeOffset.Now.AddSeconds((double)seconds);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Do the actual upload to Picasa
|
||||
/// </summary>
|
||||
|
@ -148,7 +55,7 @@ namespace GreenshotPicasaPlugin {
|
|||
settings.ClientId = PicasaCredentials.ClientId;
|
||||
settings.ClientSecret = PicasaCredentials.ClientSecret;
|
||||
|
||||
// Copy the settings from the config, which is kept in memory
|
||||
// Copy the settings from the config, which is kept in memory and on the disk
|
||||
settings.RefreshToken = Config.RefreshToken;
|
||||
settings.AccessToken = Config.AccessToken;
|
||||
settings.AccessTokenExpires = Config.AccessTokenExpires;
|
||||
|
@ -156,18 +63,18 @@ namespace GreenshotPicasaPlugin {
|
|||
try {
|
||||
// Get Refresh / Access token
|
||||
if (string.IsNullOrEmpty(settings.RefreshToken)) {
|
||||
Authenticate(settings);
|
||||
OAuth2Helper.AuthenticateViaLocalServer(settings);
|
||||
}
|
||||
|
||||
if (settings.IsAccessTokenExpired) {
|
||||
GenerateAccessToken(settings);
|
||||
OAuth2Helper.GenerateAccessToken(settings);
|
||||
}
|
||||
|
||||
var webRequest = (HttpWebRequest)NetworkHelper.CreateWebRequest(string.Format(UploadUrl, Config.UploadUser, Config.UploadAlbum));
|
||||
webRequest.Method = "POST";
|
||||
webRequest.KeepAlive = true;
|
||||
webRequest.Credentials = CredentialCache.DefaultCredentials;
|
||||
webRequest.Headers.Add("Authorization", "Bearer " + settings.AccessToken);
|
||||
OAuth2Helper.AddOAuth2Credentials(webRequest, settings);
|
||||
if (Config.AddFilename) {
|
||||
webRequest.Headers.Add("Slug", NetworkHelper.EscapeDataString(filename));
|
||||
}
|
||||
|
@ -178,7 +85,7 @@ namespace GreenshotPicasaPlugin {
|
|||
|
||||
return ParseResponse(response);
|
||||
} finally {
|
||||
// Copy the settings back to the config
|
||||
// Copy the settings back to the config, so they are stored.
|
||||
Config.RefreshToken = settings.RefreshToken;
|
||||
Config.AccessToken = settings.AccessToken;
|
||||
Config.AccessTokenExpires = settings.AccessTokenExpires;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue