Slowly starting to get Dropbox working again

This commit is contained in:
Robin Krom 2020-10-26 23:37:57 +01:00
commit 51ea5fdc94
3 changed files with 60 additions and 48 deletions

View file

@ -1,23 +1,25 @@
/* /*
* Greenshot - a free and open source screenshot tool * Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom, Francis Noel * Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom, Francis Noel
* *
* For more information see: http://getgreenshot.org/ * For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot * The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or * the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* 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.Windows.Forms; using System.Windows.Forms;
using GreenshotPlugin.Core; using GreenshotPlugin.Core;
using GreenshotPlugin.IniFile; using GreenshotPlugin.IniFile;
@ -38,10 +40,18 @@ namespace GreenshotDropboxPlugin {
[IniProperty("AfterUploadLinkToClipBoard", Description = "After upload send Dropbox link to clipboard.", DefaultValue = "true")] [IniProperty("AfterUploadLinkToClipBoard", Description = "After upload send Dropbox link to clipboard.", DefaultValue = "true")]
public bool AfterUploadLinkToClipBoard { get; set; } public bool AfterUploadLinkToClipBoard { get; set; }
[IniProperty("DropboxToken", Description = "The Dropbox token", Encrypted = true, ExcludeIfNull = true)] [IniProperty("RefreshToken", Description = "Dropbox refresh Token", Encrypted = true, ExcludeIfNull = true)]
public string DropboxToken { get; set; } public string RefreshToken { get; set; }
[IniProperty("DropboxTokenSecret", Description = "The Dropbox token secret", Encrypted = true, ExcludeIfNull = true)]
public string DropboxTokenSecret { get; set; } /// <summary>
/// AccessToken, not stored
/// </summary>
public string AccessToken { get; set; }
/// <summary>
/// AccessTokenExpires, not stored
/// </summary>
public DateTimeOffset AccessTokenExpires { get; set; }
/// <summary> /// <summary>
/// A form for token /// A form for token

View file

@ -1,26 +1,25 @@
/* /*
* Greenshot - a free and open source screenshot tool * Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom, Francis Noel * Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom, Francis Noel
* *
* For more information see: http://getgreenshot.org/ * For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot * The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or * the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* 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 GreenshotPlugin.Core; using GreenshotPlugin.Core;
using GreenshotPlugin.Core.OAuth; using GreenshotPlugin.Core.OAuth;
using GreenshotPlugin.IniFile; using GreenshotPlugin.IniFile;
@ -39,48 +38,47 @@ namespace GreenshotDropboxPlugin {
} }
public static string UploadToDropbox(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string filename) { public static string UploadToDropbox(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string filename) {
var oAuth = new OAuthSession(DropBoxCredentials.CONSUMER_KEY, DropBoxCredentials.CONSUMER_SECRET)
{ var oauth2Settings = new OAuth2Settings
BrowserSize = new Size(1080, 650), {
CheckVerifier = false, AuthUrlPattern = "https://api.dropbox.com/oauth2/authorize?response_type=token&client_id={ClientId}&state={State}&redirect_uri={RedirectUrl}&token_access_type=offline",
AccessTokenUrl = "https://api.dropbox.com/1/oauth/access_token", TokenUrl = "https://api.dropbox.com/oauth2/token",
AuthorizeUrl = "https://api.dropbox.com/1/oauth/authorize", RedirectUrl = "https://getgreenshot.org/authorize/dropbox",
RequestTokenUrl = "https://api.dropbox.com/1/oauth/request_token", CloudServiceName = "Dropbox",
LoginTitle = "Dropbox authorization", ClientId = DropBoxCredentials.CONSUMER_KEY,
Token = DropboxConfig.DropboxToken, ClientSecret = DropBoxCredentials.CONSUMER_SECRET,
TokenSecret = DropboxConfig.DropboxTokenSecret AuthorizeMode = OAuth2AuthorizeMode.JsonReceiver,
RefreshToken = DropboxConfig.RefreshToken,
AccessToken = DropboxConfig.AccessToken,
AccessTokenExpires = DropboxConfig.AccessTokenExpires
}; };
try { try
SurfaceContainer imageToUpload = new SurfaceContainer(surfaceToUpload, outputSettings, filename); {
string uploadResponse = oAuth.MakeOAuthRequest(HTTPMethod.POST, "https://api-content.dropbox.com/1/files_put/sandbox/" + OAuthSession.UrlEncode3986(filename), null, null, imageToUpload); SurfaceContainer image = new SurfaceContainer(surfaceToUpload, outputSettings, filename);
Log.DebugFormat("Upload response: {0}", uploadResponse);
IDictionary<string, object> parameters = new Dictionary<string, object>
{
{ "file", image },
{ "autorename", true },
{ "mute", true},
{ "path", filename}
};
var webRequest = OAuth2Helper.CreateOAuth2WebRequest(HTTPMethod.POST, "https://api.dropbox.com//2/files/upload", oauth2Settings);
NetworkHelper.WriteMultipartFormData(webRequest, parameters);
var response = NetworkHelper.GetResponseAsString(webRequest);
Log.DebugFormat("Upload response: {0}", response);
} catch (Exception ex) { } catch (Exception ex) {
Log.Error("Upload error: ", ex); Log.Error("Upload error: ", ex);
throw; throw;
} finally { } finally {
if (!string.IsNullOrEmpty(oAuth.Token)) { DropboxConfig.RefreshToken = oauth2Settings.RefreshToken;
DropboxConfig.DropboxToken = oAuth.Token; DropboxConfig.AccessToken = oauth2Settings.AccessToken;
} DropboxConfig.AccessTokenExpires = oauth2Settings.AccessTokenExpires;
if (!string.IsNullOrEmpty(oAuth.TokenSecret)) { DropboxConfig.IsDirty = true;
DropboxConfig.DropboxTokenSecret = oAuth.TokenSecret; IniConfig.Save();
}
} }
return null;
// Try to get a URL to the uploaded image
try {
string responseString = oAuth.MakeOAuthRequest(HTTPMethod.GET, "https://api.dropbox.com/1/shares/sandbox/" + OAuthSession.UrlEncode3986(filename), null, null, null);
if (responseString != null) {
Log.DebugFormat("Parsing output: {0}", responseString);
IDictionary<string, object> returnValues = JSONHelper.JsonDecode(responseString);
if (returnValues.ContainsKey("url")) {
return returnValues["url"] as string;
}
}
} catch (Exception ex) {
Log.Error("Can't parse response.", ex);
}
return null;
} }
} }
} }

View file

@ -221,6 +221,10 @@ namespace GreenshotPlugin.Core.OAuth {
var codeReceiver = new LocalJsonReceiver(); var codeReceiver = new LocalJsonReceiver();
IDictionary<string, string> result = codeReceiver.ReceiveCode(settings); IDictionary<string, string> result = codeReceiver.ReceiveCode(settings);
if (result == null || result.Count == 0)
{
return false;
}
foreach (var key in result.Keys) foreach (var key in result.Keys)
{ {
switch (key) switch (key)