mirror of
https://github.com/greenshot/greenshot
synced 2025-08-14 02:37:03 -07:00
BUG-1769: Added OAuth 2 for Picasa
This commit is contained in:
parent
29c7f466ec
commit
ea4631af3d
7 changed files with 660 additions and 139 deletions
|
@ -24,17 +24,102 @@ using System.Xml;
|
|||
using Greenshot.IniFile;
|
||||
using Greenshot.Plugin;
|
||||
using GreenshotPlugin.Core;
|
||||
using System.Net;
|
||||
|
||||
namespace GreenshotPicasaPlugin {
|
||||
/// <summary>
|
||||
/// Description of PicasaUtils.
|
||||
/// </summary>
|
||||
public class PicasaUtils {
|
||||
private const string GoogleAccountUri = "https://www.google.com/accounts/";
|
||||
public static class PicasaUtils {
|
||||
private const string PicasaScope = "https://picasaweb.google.com/data/";
|
||||
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(PicasaUtils));
|
||||
private static readonly PicasaConfiguration Config = IniConfig.GetIniSection<PicasaConfiguration>();
|
||||
private const string AuthUrl = "https://accounts.google.com/o/oauth2/auth?response_type={response_type}&client_id={ClientId}&redirect_uri={RedirectUrl}&state={State}&scope={scope}";
|
||||
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}";
|
||||
|
||||
private PicasaUtils() {
|
||||
/// <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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <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>
|
||||
|
@ -46,46 +131,57 @@ namespace GreenshotPicasaPlugin {
|
|||
/// <param name="filename"></param>
|
||||
/// <returns>PicasaResponse</returns>
|
||||
public static string UploadToPicasa(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string title, string filename) {
|
||||
OAuthSession oAuth = new OAuthSession(PicasaCredentials.ConsumerKey, PicasaCredentials.ConsumerSecret);
|
||||
oAuth.BrowserSize = new Size(1020, 590);
|
||||
oAuth.AccessTokenUrl = GoogleAccountUri + "OAuthGetAccessToken";
|
||||
oAuth.AuthorizeUrl = GoogleAccountUri + "OAuthAuthorizeToken";
|
||||
oAuth.RequestTokenUrl = GoogleAccountUri + "OAuthGetRequestToken";
|
||||
oAuth.LoginTitle = "Picasa authorization";
|
||||
oAuth.Token = Config.PicasaToken;
|
||||
oAuth.TokenSecret = Config.PicasaTokenSecret;
|
||||
oAuth.RequestTokenParameters.Add("scope", "https://picasaweb.google.com/data/");
|
||||
oAuth.RequestTokenParameters.Add("xoauth_displayname", "Greenshot");
|
||||
if (string.IsNullOrEmpty(oAuth.Token)) {
|
||||
if (!oAuth.Authorize()) {
|
||||
return null;
|
||||
}
|
||||
if (!string.IsNullOrEmpty(oAuth.Token)) {
|
||||
Config.PicasaToken = oAuth.Token;
|
||||
}
|
||||
if (!string.IsNullOrEmpty(oAuth.TokenSecret)) {
|
||||
Config.PicasaTokenSecret = oAuth.TokenSecret;
|
||||
}
|
||||
IniConfig.Save();
|
||||
}
|
||||
// Fill the OAuth2Settings
|
||||
OAuth2Settings settings = new OAuth2Settings();
|
||||
settings.AuthUrlPattern = AuthUrl;
|
||||
settings.TokenUrlPattern = TokenUrl;
|
||||
settings.AdditionalAttributes.Add("response_type", "code");
|
||||
settings.AdditionalAttributes.Add("scope", PicasaScope);
|
||||
settings.ClientId = PicasaCredentials.ClientId;
|
||||
settings.ClientSecret = PicasaCredentials.ClientSecret;
|
||||
|
||||
// Copy the settings from the config, which is kept in memory
|
||||
settings.RefreshToken = Config.RefreshToken;
|
||||
settings.AccessToken = Config.AccessToken;
|
||||
settings.AccessTokenExpires = Config.AccessTokenExpires;
|
||||
|
||||
try {
|
||||
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 SurfaceContainer(surfaceToUpload, outputSettings, filename));
|
||||
// Get Refresh / Access token
|
||||
if (string.IsNullOrEmpty(settings.RefreshToken)) {
|
||||
Authenticate(settings);
|
||||
}
|
||||
|
||||
if (settings.IsAccessTokenExpired) {
|
||||
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);
|
||||
if (Config.AddFilename) {
|
||||
webRequest.Headers.Add("Slug", NetworkHelper.EscapeDataString(filename));
|
||||
}
|
||||
SurfaceContainer container = new SurfaceContainer(surfaceToUpload, outputSettings, filename);
|
||||
container.Upload(webRequest);
|
||||
|
||||
string response = NetworkHelper.GetResponse(webRequest);
|
||||
|
||||
return ParseResponse(response);
|
||||
} catch (Exception ex) {
|
||||
LOG.Error("Upload error: ", ex);
|
||||
throw;
|
||||
} finally {
|
||||
if (!string.IsNullOrEmpty(oAuth.Token)) {
|
||||
Config.PicasaToken = oAuth.Token;
|
||||
}
|
||||
if (!string.IsNullOrEmpty(oAuth.TokenSecret)) {
|
||||
Config.PicasaTokenSecret = oAuth.TokenSecret;
|
||||
}
|
||||
// Copy the settings back to the config
|
||||
Config.RefreshToken = settings.RefreshToken;
|
||||
Config.AccessToken = settings.AccessToken;
|
||||
Config.AccessTokenExpires = settings.AccessTokenExpires;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse the upload URL from the response
|
||||
/// </summary>
|
||||
/// <param name="response"></param>
|
||||
/// <returns></returns>
|
||||
public static string ParseResponse(string response) {
|
||||
if (response == null) {
|
||||
return null;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue