From 51ea5fdc94d55c1677d364230917ebe2e32ce69a Mon Sep 17 00:00:00 2001 From: Robin Krom Date: Mon, 26 Oct 2020 23:37:57 +0100 Subject: [PATCH] Slowly starting to get Dropbox working again --- .../DropboxPluginConfiguration.cs | 26 +++++-- GreenshotDropboxPlugin/DropboxUtils.cs | 78 +++++++++---------- GreenshotPlugin/Core/OAuth/OAuth2Helper.cs | 4 + 3 files changed, 60 insertions(+), 48 deletions(-) diff --git a/GreenshotDropboxPlugin/DropboxPluginConfiguration.cs b/GreenshotDropboxPlugin/DropboxPluginConfiguration.cs index 1b189487b..c57d31073 100644 --- a/GreenshotDropboxPlugin/DropboxPluginConfiguration.cs +++ b/GreenshotDropboxPlugin/DropboxPluginConfiguration.cs @@ -1,23 +1,25 @@ /* * Greenshot - a free and open source screenshot tool * Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom, Francis Noel - * + * * For more information see: http://getgreenshot.org/ * The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot - * + * * 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 * the Free Software Foundation, either version 1 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ + +using System; using System.Windows.Forms; using GreenshotPlugin.Core; using GreenshotPlugin.IniFile; @@ -38,10 +40,18 @@ namespace GreenshotDropboxPlugin { [IniProperty("AfterUploadLinkToClipBoard", Description = "After upload send Dropbox link to clipboard.", DefaultValue = "true")] public bool AfterUploadLinkToClipBoard { get; set; } - [IniProperty("DropboxToken", Description = "The Dropbox token", Encrypted = true, ExcludeIfNull = true)] - public string DropboxToken { get; set; } - [IniProperty("DropboxTokenSecret", Description = "The Dropbox token secret", Encrypted = true, ExcludeIfNull = true)] - public string DropboxTokenSecret { get; set; } + [IniProperty("RefreshToken", Description = "Dropbox refresh Token", Encrypted = true, ExcludeIfNull = true)] + public string RefreshToken { get; set; } + + /// + /// AccessToken, not stored + /// + public string AccessToken { get; set; } + + /// + /// AccessTokenExpires, not stored + /// + public DateTimeOffset AccessTokenExpires { get; set; } /// /// A form for token diff --git a/GreenshotDropboxPlugin/DropboxUtils.cs b/GreenshotDropboxPlugin/DropboxUtils.cs index 51f1dfc6f..c864c8cd3 100644 --- a/GreenshotDropboxPlugin/DropboxUtils.cs +++ b/GreenshotDropboxPlugin/DropboxUtils.cs @@ -1,26 +1,25 @@ /* * Greenshot - a free and open source screenshot tool * Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom, Francis Noel - * + * * For more information see: http://getgreenshot.org/ * The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot - * + * * 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 * the Free Software Foundation, either version 1 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ using System; using System.Collections.Generic; -using System.Drawing; using GreenshotPlugin.Core; using GreenshotPlugin.Core.OAuth; using GreenshotPlugin.IniFile; @@ -39,48 +38,47 @@ namespace GreenshotDropboxPlugin { } public static string UploadToDropbox(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string filename) { - var oAuth = new OAuthSession(DropBoxCredentials.CONSUMER_KEY, DropBoxCredentials.CONSUMER_SECRET) - { - BrowserSize = new Size(1080, 650), - CheckVerifier = false, - AccessTokenUrl = "https://api.dropbox.com/1/oauth/access_token", - AuthorizeUrl = "https://api.dropbox.com/1/oauth/authorize", - RequestTokenUrl = "https://api.dropbox.com/1/oauth/request_token", - LoginTitle = "Dropbox authorization", - Token = DropboxConfig.DropboxToken, - TokenSecret = DropboxConfig.DropboxTokenSecret + + var oauth2Settings = new OAuth2Settings + { + AuthUrlPattern = "https://api.dropbox.com/oauth2/authorize?response_type=token&client_id={ClientId}&state={State}&redirect_uri={RedirectUrl}&token_access_type=offline", + TokenUrl = "https://api.dropbox.com/oauth2/token", + RedirectUrl = "https://getgreenshot.org/authorize/dropbox", + CloudServiceName = "Dropbox", + ClientId = DropBoxCredentials.CONSUMER_KEY, + ClientSecret = DropBoxCredentials.CONSUMER_SECRET, + AuthorizeMode = OAuth2AuthorizeMode.JsonReceiver, + RefreshToken = DropboxConfig.RefreshToken, + AccessToken = DropboxConfig.AccessToken, + AccessTokenExpires = DropboxConfig.AccessTokenExpires }; - 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); - Log.DebugFormat("Upload response: {0}", uploadResponse); + try + { + SurfaceContainer image = new SurfaceContainer(surfaceToUpload, outputSettings, filename); + + IDictionary parameters = new Dictionary + { + { "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) { Log.Error("Upload error: ", ex); throw; } finally { - if (!string.IsNullOrEmpty(oAuth.Token)) { - DropboxConfig.DropboxToken = oAuth.Token; - } - if (!string.IsNullOrEmpty(oAuth.TokenSecret)) { - DropboxConfig.DropboxTokenSecret = oAuth.TokenSecret; - } + DropboxConfig.RefreshToken = oauth2Settings.RefreshToken; + DropboxConfig.AccessToken = oauth2Settings.AccessToken; + DropboxConfig.AccessTokenExpires = oauth2Settings.AccessTokenExpires; + DropboxConfig.IsDirty = true; + IniConfig.Save(); } - - // 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 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; + return null; } } } diff --git a/GreenshotPlugin/Core/OAuth/OAuth2Helper.cs b/GreenshotPlugin/Core/OAuth/OAuth2Helper.cs index 2189f09a1..2436fb90d 100644 --- a/GreenshotPlugin/Core/OAuth/OAuth2Helper.cs +++ b/GreenshotPlugin/Core/OAuth/OAuth2Helper.cs @@ -221,6 +221,10 @@ namespace GreenshotPlugin.Core.OAuth { var codeReceiver = new LocalJsonReceiver(); IDictionary result = codeReceiver.ReceiveCode(settings); + if (result == null || result.Count == 0) + { + return false; + } foreach (var key in result.Keys) { switch (key)