From 1d8caac431b3368dd10b9d6d31a4dd4474c28a4a Mon Sep 17 00:00:00 2001 From: Jaex Date: Sun, 8 Jun 2014 22:11:44 +0300 Subject: [PATCH] Dropbox OAuth2 implementation interim commit --- ShareX/UploadTask.cs | 2 +- UploadersLib/FileUploaders/Box.cs | 22 ++- UploadersLib/FileUploaders/Dropbox.cs | 139 +++++++++++------- UploadersLib/FileUploaders/GoogleDrive.cs | 17 ++- UploadersLib/GUI/DropboxFilesForm.cs | 2 +- UploadersLib/GUI/UploadersConfigFormHelper.cs | 82 +++++------ UploadersLib/HelperClasses/OAuth/IOAuth.cs | 8 +- UploadersLib/HelperClasses/OAuth/IOAuth2.cs | 8 +- .../HelperClasses/OAuth/IOAuth2Basic.cs | 34 +++++ .../OAuth/{IOAuth2Simple.cs => IOAuthBase.cs} | 4 +- UploadersLib/ImageUploaders/Imgur_v3.cs | 20 ++- UploadersLib/ImageUploaders/Picasa.cs | 17 ++- UploadersLib/TextUploaders/Gist.cs | 2 +- .../URLShorteners/BitlyURLShortener.cs | 2 +- UploadersLib/UploadersConfig.cs | 4 +- UploadersLib/UploadersLib.csproj | 4 +- 16 files changed, 212 insertions(+), 155 deletions(-) create mode 100644 UploadersLib/HelperClasses/OAuth/IOAuth2Basic.cs rename UploadersLib/HelperClasses/OAuth/{IOAuth2Simple.cs => IOAuthBase.cs} (94%) diff --git a/ShareX/UploadTask.cs b/ShareX/UploadTask.cs index a4dff773e..96bf56fa8 100644 --- a/ShareX/UploadTask.cs +++ b/ShareX/UploadTask.cs @@ -805,7 +805,7 @@ public UploadResult UploadFile(Stream stream, string fileName) switch (fileDestination) { case FileDestination.Dropbox: - fileUploader = new Dropbox(Program.UploadersConfig.DropboxOAuthInfo, Program.UploadersConfig.DropboxAccountInfo) + fileUploader = new Dropbox(Program.UploadersConfig.DropboxOAuth2Info, Program.UploadersConfig.DropboxAccountInfo) { UploadPath = NameParser.Parse(NameParserType.URL, Dropbox.TidyUploadPath(Program.UploadersConfig.DropboxUploadPath)), AutoCreateShareableLink = Program.UploadersConfig.DropboxAutoCreateShareableLink, diff --git a/UploadersLib/FileUploaders/Box.cs b/UploadersLib/FileUploaders/Box.cs index 44b2f8c04..4b7acdd98 100644 --- a/UploadersLib/FileUploaders/Box.cs +++ b/UploadersLib/FileUploaders/Box.cs @@ -113,6 +113,13 @@ public bool RefreshAccessToken() return false; } + private NameValueCollection GetAuthHeaders() + { + NameValueCollection headers = new NameValueCollection(); + headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token); + return headers; + } + public bool CheckAuthorization() { if (OAuth2Info.CheckOAuth(AuthInfo)) @@ -146,10 +153,7 @@ public BoxFileInfo GetFiles(string id) string url = string.Format("https://api.box.com/2.0/folders/{0}/items", id); - NameValueCollection headers = new NameValueCollection(); - headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token); - - string response = SendRequest(HttpMethod.GET, url, headers: headers); + string response = SendRequest(HttpMethod.GET, url, headers: GetAuthHeaders()); if (!string.IsNullOrEmpty(response)) { @@ -161,10 +165,7 @@ public BoxFileInfo GetFiles(string id) public string CreateSharedLink(string id) { - NameValueCollection headers = new NameValueCollection(); - headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token); - - string response = SendRequest(HttpMethod.PUT, "https://api.box.com/2.0/files/" + id, "{\"shared_link\": {\"access\": \"open\"}}", headers: headers); + string response = SendRequest(HttpMethod.PUT, "https://api.box.com/2.0/files/" + id, "{\"shared_link\": {\"access\": \"open\"}}", headers: GetAuthHeaders()); if (!string.IsNullOrEmpty(response)) { @@ -194,10 +195,7 @@ public override UploadResult Upload(Stream stream, string fileName) Dictionary args = new Dictionary(); args.Add("parent_id", FolderID); - NameValueCollection headers = new NameValueCollection(); - headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token); - - UploadResult result = UploadData(stream, "https://upload.box.com/api/2.0/files/content", fileName, "filename", args, headers: headers); + UploadResult result = UploadData(stream, "https://upload.box.com/api/2.0/files/content", fileName, "filename", args, headers: GetAuthHeaders()); if (result.IsSuccess) { diff --git a/UploadersLib/FileUploaders/Dropbox.cs b/UploadersLib/FileUploaders/Dropbox.cs index f5b3c7fa9..fa42a4542 100644 --- a/UploadersLib/FileUploaders/Dropbox.cs +++ b/UploadersLib/FileUploaders/Dropbox.cs @@ -27,15 +27,16 @@ using Newtonsoft.Json; using System; using System.Collections.Generic; +using System.Collections.Specialized; using System.IO; using System.Text.RegularExpressions; using UploadersLib.HelperClasses; namespace UploadersLib.FileUploaders { - public sealed class Dropbox : FileUploader, IOAuth + public sealed class Dropbox : FileUploader, IOAuth2Basic { - public OAuthInfo AuthInfo { get; set; } + public OAuth2Info AuthInfo { get; set; } public DropboxAccountInfo AccountInfo { get; set; } public string UploadPath { get; set; } public bool AutoCreateShareableLink { get; set; } @@ -44,6 +45,7 @@ public sealed class Dropbox : FileUploader, IOAuth private const string APIVersion = "1"; private const string Root = "dropbox"; // dropbox or sandbox + private const string URLWEB = "https://www.dropbox.com/" + APIVersion; private const string URLAPI = "https://api.dropbox.com/" + APIVersion; private const string URLAPIContent = "https://api-content.dropbox.com/" + APIVersion; @@ -58,47 +60,85 @@ public sealed class Dropbox : FileUploader, IOAuth private const string URLPublicDirect = "https://dl.dropboxusercontent.com/u"; private const string URLShareDirect = "https://dl.dropboxusercontent.com/s"; - private const string URLRequestToken = URLAPI + "/oauth/request_token"; - private const string URLAuthorize = "https://www.dropbox.com/" + APIVersion + "/oauth/authorize"; - private const string URLAccessToken = URLAPI + "/oauth/access_token"; - - public Dropbox(OAuthInfo oauth) + public Dropbox(OAuth2Info oauth) { AuthInfo = oauth; } - public Dropbox(OAuthInfo oauth, DropboxAccountInfo accountInfo) + public Dropbox(OAuth2Info oauth, DropboxAccountInfo accountInfo) : this(oauth) { AccountInfo = accountInfo; } - // https://www.dropbox.com/developers/core/api#request-token - // https://www.dropbox.com/developers/core/api#authorize + // https://www.dropbox.com/developers/core/docs#oa2-authorize public string GetAuthorizationURL() { - return GetAuthorizationURL(URLRequestToken, URLAuthorize, AuthInfo); + Dictionary args = new Dictionary(); + args.Add("response_type", "code"); + args.Add("client_id", AuthInfo.Client_ID); + + return CreateQuery(URLWEB + "/oauth2/authorize", args); } - // https://www.dropbox.com/developers/core/api#access-token + // https://www.dropbox.com/developers/core/docs#oa2-token + public bool GetAccessToken(string code) + { + Dictionary args = new Dictionary(); + args.Add("client_id", AuthInfo.Client_ID); + args.Add("client_secret", AuthInfo.Client_Secret); + args.Add("grant_type", "authorization_code"); + args.Add("code", code); + + string response = SendRequest(HttpMethod.POST, URLAPI + "/oauth2/token", args); + + if (!string.IsNullOrEmpty(response)) + { + OAuth2Token token = JsonConvert.DeserializeObject(response); + + if (token != null && !string.IsNullOrEmpty(token.access_token)) + { + AuthInfo.Token = token; + return true; + } + } + + return false; + } + + private NameValueCollection GetAuthHeaders() + { + NameValueCollection headers = new NameValueCollection(); + headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token); + return headers; + } + + /* OAuth 1.0 + // https://www.dropbox.com/developers/core/docs#request-token + // https://www.dropbox.com/developers/core/docs#authorize + public string GetAuthorizationURL() + { + return GetAuthorizationURL(URLAPI + "/oauth/request_token", URLWEB + "/oauth/authorize", AuthInfo); + } + + // https://www.dropbox.com/developers/core/docs#access-token public bool GetAccessToken(string verificationCode = null) { AuthInfo.AuthVerifier = verificationCode; - return GetAccessToken(URLAccessToken, AuthInfo); + return GetAccessToken(URLAPI + "/oauth/access_token", AuthInfo); } + */ #region Dropbox accounts - // https://www.dropbox.com/developers/core/api#account-info + // https://www.dropbox.com/developers/core/docs#account-info public DropboxAccountInfo GetAccountInfo() { DropboxAccountInfo account = null; - if (OAuthInfo.CheckOAuth(AuthInfo)) + if (OAuth2Info.CheckOAuth(AuthInfo)) { - string query = OAuthManager.GenerateQuery(URLAccountInfo, null, HttpMethod.GET, AuthInfo); - - string response = SendRequest(HttpMethod.GET, query); + string response = SendRequest(HttpMethod.GET, URLAccountInfo, headers: GetAuthHeaders()); if (!string.IsNullOrEmpty(response)) { @@ -118,23 +158,22 @@ public DropboxAccountInfo GetAccountInfo() #region Files and metadata - // https://www.dropbox.com/developers/core/api#files-GET + // https://www.dropbox.com/developers/core/docs#files-GET public bool DownloadFile(string path, Stream downloadStream) { - if (!string.IsNullOrEmpty(path) && OAuthInfo.CheckOAuth(AuthInfo)) + if (!string.IsNullOrEmpty(path) && OAuth2Info.CheckOAuth(AuthInfo)) { string url = Helpers.CombineURL(URLFiles, Helpers.URLPathEncode(path)); - string query = OAuthManager.GenerateQuery(url, null, HttpMethod.GET, AuthInfo); - return SendRequest(HttpMethod.GET, downloadStream, query); + return SendRequest(HttpMethod.GET, downloadStream, url, headers: GetAuthHeaders()); } return false; } - // https://www.dropbox.com/developers/core/api#files-POST + // https://www.dropbox.com/developers/core/docs#files_put public UploadResult UploadFile(Stream stream, string path, string fileName, bool createShareableURL = false, DropboxURLType urlType = DropboxURLType.Default) { - if (!OAuthInfo.CheckOAuth(AuthInfo)) + if (!OAuth2Info.CheckOAuth(AuthInfo)) { Errors.Add("Dropbox login is required."); return null; @@ -145,10 +184,8 @@ public UploadResult UploadFile(Stream stream, string path, string fileName, bool Dictionary args = new Dictionary(); args.Add("file", fileName); - string query = OAuthManager.GenerateQuery(url, args, HttpMethod.POST, AuthInfo); - // There's a 150MB limit to all uploads through the API. - UploadResult result = UploadData(stream, query, fileName); + UploadResult result = UploadData(stream, url, fileName, arguments: args, headers: GetAuthHeaders()); if (result.IsSuccess) { @@ -170,21 +207,19 @@ public UploadResult UploadFile(Stream stream, string path, string fileName, bool return result; } - // https://www.dropbox.com/developers/core/api#metadata + // https://www.dropbox.com/developers/core/docs#metadata public DropboxContentInfo GetMetadata(string path, bool list) { DropboxContentInfo contentInfo = null; - if (OAuthInfo.CheckOAuth(AuthInfo)) + if (OAuth2Info.CheckOAuth(AuthInfo)) { string url = Helpers.CombineURL(URLMetaData, Helpers.URLPathEncode(path)); Dictionary args = new Dictionary(); args.Add("list", list ? "true" : "false"); - string query = OAuthManager.GenerateQuery(url, args, HttpMethod.GET, AuthInfo); - - string response = SendRequest(HttpMethod.GET, query); + string response = SendRequest(HttpMethod.GET, url, args, headers: GetAuthHeaders()); if (!string.IsNullOrEmpty(response)) { @@ -201,19 +236,17 @@ public bool IsExists(string path) return contentInfo != null && !contentInfo.Is_deleted; } - // https://www.dropbox.com/developers/core/api#shares + // https://www.dropbox.com/developers/core/docs#shares public string CreateShareableLink(string path, DropboxURLType urlType) { - if (!string.IsNullOrEmpty(path) && OAuthInfo.CheckOAuth(AuthInfo)) + if (!string.IsNullOrEmpty(path) && OAuth2Info.CheckOAuth(AuthInfo)) { string url = Helpers.CombineURL(URLShares, Helpers.URLPathEncode(path)); Dictionary args = new Dictionary(); args.Add("short_url", urlType == DropboxURLType.Shortened ? "true" : "false"); - string query = OAuthManager.GenerateQuery(url, args, HttpMethod.POST, AuthInfo); - - string response = SendRequest(HttpMethod.POST, query); + string response = SendRequest(HttpMethod.POST, url, args, headers: GetAuthHeaders()); if (!string.IsNullOrEmpty(response)) { @@ -245,21 +278,19 @@ public string CreateShareableLink(string path, DropboxURLType urlType) #region File operations - // https://www.dropbox.com/developers/core/api#fileops-copy + // https://www.dropbox.com/developers/core/docs#fileops-copy public DropboxContentInfo Copy(string from_path, string to_path) { DropboxContentInfo contentInfo = null; - if (!string.IsNullOrEmpty(from_path) && !string.IsNullOrEmpty(to_path) && OAuthInfo.CheckOAuth(AuthInfo)) + if (!string.IsNullOrEmpty(from_path) && !string.IsNullOrEmpty(to_path) && OAuth2Info.CheckOAuth(AuthInfo)) { Dictionary args = new Dictionary(); args.Add("root", Root); args.Add("from_path", from_path); args.Add("to_path", to_path); - string query = OAuthManager.GenerateQuery(URLCopy, args, HttpMethod.POST, AuthInfo); - - string response = SendRequest(HttpMethod.POST, query); + string response = SendRequest(HttpMethod.POST, URLCopy, args, headers: GetAuthHeaders()); if (!string.IsNullOrEmpty(response)) { @@ -270,20 +301,18 @@ public DropboxContentInfo Copy(string from_path, string to_path) return contentInfo; } - // https://www.dropbox.com/developers/core/api#fileops-create-folder + // https://www.dropbox.com/developers/core/docs#fileops-create-folder public DropboxContentInfo CreateFolder(string path) { DropboxContentInfo contentInfo = null; - if (!string.IsNullOrEmpty(path) && OAuthInfo.CheckOAuth(AuthInfo)) + if (!string.IsNullOrEmpty(path) && OAuth2Info.CheckOAuth(AuthInfo)) { Dictionary args = new Dictionary(); args.Add("root", Root); args.Add("path", path); - string query = OAuthManager.GenerateQuery(URLCreateFolder, args, HttpMethod.POST, AuthInfo); - - string response = SendRequest(HttpMethod.POST, query); + string response = SendRequest(HttpMethod.POST, URLCreateFolder, args, headers: GetAuthHeaders()); if (!string.IsNullOrEmpty(response)) { @@ -294,20 +323,18 @@ public DropboxContentInfo CreateFolder(string path) return contentInfo; } - // https://www.dropbox.com/developers/core/api#fileops-delete + // https://www.dropbox.com/developers/core/docs#fileops-delete public DropboxContentInfo Delete(string path) { DropboxContentInfo contentInfo = null; - if (!string.IsNullOrEmpty(path) && OAuthInfo.CheckOAuth(AuthInfo)) + if (!string.IsNullOrEmpty(path) && OAuth2Info.CheckOAuth(AuthInfo)) { Dictionary args = new Dictionary(); args.Add("root", Root); args.Add("path", path); - string query = OAuthManager.GenerateQuery(URLDelete, args, HttpMethod.POST, AuthInfo); - - string response = SendRequest(HttpMethod.POST, query); + string response = SendRequest(HttpMethod.POST, URLDelete, args, headers: GetAuthHeaders()); if (!string.IsNullOrEmpty(response)) { @@ -318,21 +345,19 @@ public DropboxContentInfo Delete(string path) return contentInfo; } - // https://www.dropbox.com/developers/core/api#fileops-move + // https://www.dropbox.com/developers/core/docs#fileops-move public DropboxContentInfo Move(string from_path, string to_path) { DropboxContentInfo contentInfo = null; - if (!string.IsNullOrEmpty(from_path) && !string.IsNullOrEmpty(to_path) && OAuthInfo.CheckOAuth(AuthInfo)) + if (!string.IsNullOrEmpty(from_path) && !string.IsNullOrEmpty(to_path) && OAuth2Info.CheckOAuth(AuthInfo)) { Dictionary args = new Dictionary(); args.Add("root", Root); args.Add("from_path", from_path); args.Add("to_path", to_path); - string query = OAuthManager.GenerateQuery(URLMove, args, HttpMethod.POST, AuthInfo); - - string response = SendRequest(HttpMethod.POST, query); + string response = SendRequest(HttpMethod.POST, URLMove, args, headers: GetAuthHeaders()); if (!string.IsNullOrEmpty(response)) { diff --git a/UploadersLib/FileUploaders/GoogleDrive.cs b/UploadersLib/FileUploaders/GoogleDrive.cs index f460b4055..a3fc0e0ce 100644 --- a/UploadersLib/FileUploaders/GoogleDrive.cs +++ b/UploadersLib/FileUploaders/GoogleDrive.cs @@ -104,6 +104,13 @@ public bool RefreshAccessToken() return false; } + private NameValueCollection GetAuthHeaders() + { + NameValueCollection headers = new NameValueCollection(); + headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token); + return headers; + } + public bool CheckAuthorization() { if (OAuth2Info.CheckOAuth(AuthInfo)) @@ -125,9 +132,6 @@ public bool CheckAuthorization() public void SetPermissions(string fileID, GoogleDrivePermissionRole role, GoogleDrivePermissionType type, string value, bool withLink) { - NameValueCollection headers = new NameValueCollection(); - headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token); - string url = string.Format("https://www.googleapis.com/drive/v2/files/{0}/permissions", fileID); string json = JsonConvert.SerializeObject(new @@ -138,7 +142,7 @@ public void SetPermissions(string fileID, GoogleDrivePermissionRole role, Google withLink = withLink.ToString() }); - string response = SendPostRequestJSON(url, json, headers: headers); + string response = SendPostRequestJSON(url, json, headers: GetAuthHeaders()); } public override UploadResult Upload(Stream stream, string fileName) @@ -148,13 +152,10 @@ public override UploadResult Upload(Stream stream, string fileName) return null; } - NameValueCollection headers = new NameValueCollection(); - headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token); - Dictionary args = new Dictionary(); args.Add("title", fileName); - UploadResult result = UploadData(stream, "https://www.googleapis.com/upload/drive/v2/files", fileName, "file", args, headers: headers); + UploadResult result = UploadData(stream, "https://www.googleapis.com/upload/drive/v2/files", fileName, "file", args, headers: GetAuthHeaders()); if (!string.IsNullOrEmpty(result.Response)) { diff --git a/UploadersLib/GUI/DropboxFilesForm.cs b/UploadersLib/GUI/DropboxFilesForm.cs index c6f53bcf3..b65812f82 100644 --- a/UploadersLib/GUI/DropboxFilesForm.cs +++ b/UploadersLib/GUI/DropboxFilesForm.cs @@ -44,7 +44,7 @@ public partial class DropboxFilesForm : Form private ImageListManager ilm; private bool isSelectedFile, isSelectedPublic; - public DropboxFilesForm(OAuthInfo oauth, string path, DropboxAccountInfo accountInfo) + public DropboxFilesForm(OAuth2Info oauth, string path, DropboxAccountInfo accountInfo) { InitializeComponent(); Icon = Resources.Dropbox; diff --git a/UploadersLib/GUI/UploadersConfigFormHelper.cs b/UploadersLib/GUI/UploadersConfigFormHelper.cs index 814562436..bc69a5519 100644 --- a/UploadersLib/GUI/UploadersConfigFormHelper.cs +++ b/UploadersLib/GUI/UploadersConfigFormHelper.cs @@ -436,9 +436,9 @@ public void PicasaRefreshAlbumList() public void DropboxOpenFiles() { - if (OAuthInfo.CheckOAuth(Config.DropboxOAuthInfo)) + if (OAuth2Info.CheckOAuth(Config.DropboxOAuth2Info)) { - using (DropboxFilesForm filesForm = new DropboxFilesForm(Config.DropboxOAuthInfo, GetDropboxUploadPath(), Config.DropboxAccountInfo)) + using (DropboxFilesForm filesForm = new DropboxFilesForm(Config.DropboxOAuth2Info, GetDropboxUploadPath(), Config.DropboxAccountInfo)) { if (filesForm.ShowDialog() == DialogResult.OK) { @@ -452,13 +452,13 @@ public void DropboxAuthOpen() { try { - OAuthInfo oauth = new OAuthInfo(APIKeys.DropboxConsumerKey, APIKeys.DropboxConsumerSecret); + OAuth2Info oauth = new OAuth2Info(APIKeys.DropboxConsumerKey, APIKeys.DropboxConsumerSecret); string url = new Dropbox(oauth).GetAuthorizationURL(); if (!string.IsNullOrEmpty(url)) { - Config.DropboxOAuthInfo = oauth; + Config.DropboxOAuth2Info = oauth; Helpers.OpenURL(url); btnDropboxCompleteAuth.Enabled = true; DebugHelper.WriteLine("DropboxAuthOpen - Authorization URL is opened: " + url); @@ -476,51 +476,51 @@ public void DropboxAuthOpen() public void DropboxAuthComplete() { - try - { - if (Config.DropboxOAuthInfo != null && !string.IsNullOrEmpty(Config.DropboxOAuthInfo.AuthToken) && !string.IsNullOrEmpty(Config.DropboxOAuthInfo.AuthSecret)) - { - Dropbox dropbox = new Dropbox(Config.DropboxOAuthInfo); - bool result = dropbox.GetAccessToken(); + /* try + { + if (Config.DropboxOAuth2Info != null && !string.IsNullOrEmpty(Config.DropboxOAuth2Info.AuthToken) && !string.IsNullOrEmpty(Config.DropboxOAuth2Info.AuthSecret)) + { + Dropbox dropbox = new Dropbox(Config.DropboxOAuth2Info); + bool result = dropbox.GetAccessToken(); - if (result) - { - DropboxAccountInfo account = dropbox.GetAccountInfo(); + if (result) + { + DropboxAccountInfo account = dropbox.GetAccountInfo(); - if (account != null) - { - Config.DropboxAccountInfo = account; - Config.DropboxUploadPath = txtDropboxPath.Text; - UpdateDropboxStatus(); - MessageBox.Show("Login successful.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); - return; - } + if (account != null) + { + Config.DropboxAccountInfo = account; + Config.DropboxUploadPath = txtDropboxPath.Text; + UpdateDropboxStatus(); + MessageBox.Show("Login successful.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); + return; + } - MessageBox.Show("GetAccountInfo failed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - else - { - MessageBox.Show("Login failed.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - else - { - MessageBox.Show("You must give access from Authorize page first.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } + MessageBox.Show("GetAccountInfo failed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + else + { + MessageBox.Show("Login failed.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + else + { + MessageBox.Show("You must give access from Authorize page first.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } - Config.DropboxOAuthInfo = null; - UpdateDropboxStatus(); - } - catch (Exception ex) - { - DebugHelper.WriteException(ex); - MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } + Config.DropboxOAut2hInfo = null; + UpdateDropboxStatus(); + } + catch (Exception ex) + { + DebugHelper.WriteException(ex); + MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } */ } private void UpdateDropboxStatus() { - if (OAuthInfo.CheckOAuth(Config.DropboxOAuthInfo) && Config.DropboxAccountInfo != null) + if (OAuth2Info.CheckOAuth(Config.DropboxOAuth2Info) && Config.DropboxAccountInfo != null) { StringBuilder sb = new StringBuilder(); sb.AppendLine("Login status: Successful"); diff --git a/UploadersLib/HelperClasses/OAuth/IOAuth.cs b/UploadersLib/HelperClasses/OAuth/IOAuth.cs index 7d9bd822f..442c70ba9 100644 --- a/UploadersLib/HelperClasses/OAuth/IOAuth.cs +++ b/UploadersLib/HelperClasses/OAuth/IOAuth.cs @@ -25,12 +25,8 @@ namespace UploadersLib.HelperClasses { - public interface IOAuth + public interface IOAuth : IOAuthBase { - OAuthInfo AuthInfo { get; set; } - - string GetAuthorizationURL(); - - bool GetAccessToken(string verificationCode); + OAuthInfo AuthInfo { get; } } } \ No newline at end of file diff --git a/UploadersLib/HelperClasses/OAuth/IOAuth2.cs b/UploadersLib/HelperClasses/OAuth/IOAuth2.cs index 5780adcd6..d549fa8fe 100644 --- a/UploadersLib/HelperClasses/OAuth/IOAuth2.cs +++ b/UploadersLib/HelperClasses/OAuth/IOAuth2.cs @@ -25,14 +25,8 @@ namespace UploadersLib.HelperClasses { - public interface IOAuth2 + public interface IOAuth2 : IOAuth2Basic { - OAuth2Info AuthInfo { get; set; } - - string GetAuthorizationURL(); - - bool GetAccessToken(string pin); - bool RefreshAccessToken(); bool CheckAuthorization(); diff --git a/UploadersLib/HelperClasses/OAuth/IOAuth2Basic.cs b/UploadersLib/HelperClasses/OAuth/IOAuth2Basic.cs new file mode 100644 index 000000000..791fa2550 --- /dev/null +++ b/UploadersLib/HelperClasses/OAuth/IOAuth2Basic.cs @@ -0,0 +1,34 @@ +#region License Information (GPL v3) + +/* + ShareX - A program that allows you to take screenshots and share any file type + Copyright (C) 2007-2014 ShareX Developers + + 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 2 + 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, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + Optionally you can also view the license at . +*/ + +#endregion License Information (GPL v3) + +using System.Collections.Specialized; + +namespace UploadersLib.HelperClasses +{ + public interface IOAuth2Basic : IOAuthBase + { + OAuth2Info AuthInfo { get; } + } +} \ No newline at end of file diff --git a/UploadersLib/HelperClasses/OAuth/IOAuth2Simple.cs b/UploadersLib/HelperClasses/OAuth/IOAuthBase.cs similarity index 94% rename from UploadersLib/HelperClasses/OAuth/IOAuth2Simple.cs rename to UploadersLib/HelperClasses/OAuth/IOAuthBase.cs index 46b924db8..69bd89e80 100644 --- a/UploadersLib/HelperClasses/OAuth/IOAuth2Simple.cs +++ b/UploadersLib/HelperClasses/OAuth/IOAuthBase.cs @@ -25,10 +25,8 @@ namespace UploadersLib.HelperClasses { - public interface IOAuth2Simple + public interface IOAuthBase { - OAuth2Info AuthInfo { get; } - string GetAuthorizationURL(); bool GetAccessToken(string code); diff --git a/UploadersLib/ImageUploaders/Imgur_v3.cs b/UploadersLib/ImageUploaders/Imgur_v3.cs index 7185d371f..810430740 100644 --- a/UploadersLib/ImageUploaders/Imgur_v3.cs +++ b/UploadersLib/ImageUploaders/Imgur_v3.cs @@ -122,6 +122,13 @@ public bool RefreshAccessToken() return false; } + private NameValueCollection GetAuthHeaders() + { + NameValueCollection headers = new NameValueCollection(); + headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token); + return headers; + } + public bool CheckAuthorization() { if (OAuth2Info.CheckOAuth(AuthInfo)) @@ -145,9 +152,7 @@ public List GetAlbums() { if (CheckAuthorization()) { - NameValueCollection headers = new NameValueCollection(); - headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token); - string response = SendRequest(HttpMethod.GET, "https://api.imgur.com/3/account/me/albums", null, ResponseType.Text, headers, null); + string response = SendRequest(HttpMethod.GET, "https://api.imgur.com/3/account/me/albums", headers: GetAuthHeaders()); if (!string.IsNullOrEmpty(response)) { @@ -171,7 +176,7 @@ public List GetAlbums() public override UploadResult Upload(Stream stream, string fileName) { Dictionary args = new Dictionary(); - NameValueCollection headers = new NameValueCollection(); + NameValueCollection headers; if (UploadMethod == AccountType.User) { @@ -185,14 +190,15 @@ public override UploadResult Upload(Stream stream, string fileName) args.Add("album", UploadAlbumID); } - headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token); + headers = GetAuthHeaders(); } - else if (UploadMethod == AccountType.Anonymous) + else { + headers = new NameValueCollection(); headers.Add("Authorization", "Client-ID " + AuthInfo.Client_ID); } - UploadResult result = UploadData(stream, "https://api.imgur.com/3/image", fileName, "image", args, null, headers); + UploadResult result = UploadData(stream, "https://api.imgur.com/3/image", fileName, "image", args, headers: headers); if (!string.IsNullOrEmpty(result.Response)) { diff --git a/UploadersLib/ImageUploaders/Picasa.cs b/UploadersLib/ImageUploaders/Picasa.cs index 8123c503d..40ce77bec 100644 --- a/UploadersLib/ImageUploaders/Picasa.cs +++ b/UploadersLib/ImageUploaders/Picasa.cs @@ -109,6 +109,13 @@ public bool RefreshAccessToken() return false; } + private NameValueCollection GetAuthHeaders() + { + NameValueCollection headers = new NameValueCollection(); + headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token); + return headers; + } + public bool CheckAuthorization() { if (OAuth2Info.CheckOAuth(AuthInfo)) @@ -134,10 +141,7 @@ public List GetAlbumList() List albumList = new List(); - NameValueCollection headers = new NameValueCollection(); - headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token); - - string response = SendRequest(HttpMethod.GET, "https://picasaweb.google.com/data/feed/api/user/default", null, ResponseType.Text, headers, null); + string response = SendRequest(HttpMethod.GET, "https://picasaweb.google.com/data/feed/api/user/default", headers: GetAuthHeaders()); if (!string.IsNullOrEmpty(response)) { @@ -173,11 +177,10 @@ public override UploadResult Upload(Stream stream, string fileName) string url = string.Format("https://picasaweb.google.com/data/feed/api/user/default/albumid/" + AlbumID); string contentType = Helpers.GetMimeType(fileName); - NameValueCollection headers = new NameValueCollection(); - headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token); + NameValueCollection headers = GetAuthHeaders(); headers.Add("Slug", Helpers.URLEncode(fileName)); - ur.Response = SendPostRequestStream(url, stream, contentType, null, headers); + ur.Response = SendPostRequestStream(url, stream, contentType, headers: headers); XDocument xd = XDocument.Parse(ur.Response); diff --git a/UploadersLib/TextUploaders/Gist.cs b/UploadersLib/TextUploaders/Gist.cs index 0bb5fda7c..a0357d960 100644 --- a/UploadersLib/TextUploaders/Gist.cs +++ b/UploadersLib/TextUploaders/Gist.cs @@ -31,7 +31,7 @@ namespace UploadersLib.TextUploaders { - public sealed class Gist : TextUploader, IOAuth2Simple + public sealed class Gist : TextUploader, IOAuth2Basic { private const string URLAPI = "https://api.github.com/"; private const string URLGists = URLAPI + "gists"; diff --git a/UploadersLib/URLShorteners/BitlyURLShortener.cs b/UploadersLib/URLShorteners/BitlyURLShortener.cs index c4c782334..56c8bae4b 100644 --- a/UploadersLib/URLShorteners/BitlyURLShortener.cs +++ b/UploadersLib/URLShorteners/BitlyURLShortener.cs @@ -31,7 +31,7 @@ namespace UploadersLib.URLShorteners { - public sealed class BitlyURLShortener : URLShortener, IOAuth2Simple + public sealed class BitlyURLShortener : URLShortener, IOAuth2Basic { private const string URLAPI = "https://api-ssl.bitly.com/"; private const string URLAccessToken = URLAPI + "oauth/access_token"; diff --git a/UploadersLib/UploadersConfig.cs b/UploadersLib/UploadersConfig.cs index dd518e271..02fbb2834 100644 --- a/UploadersLib/UploadersConfig.cs +++ b/UploadersLib/UploadersConfig.cs @@ -116,7 +116,7 @@ public class UploadersConfig : SettingsBase // Dropbox - public OAuthInfo DropboxOAuthInfo = null; + public OAuth2Info DropboxOAuth2Info = null; public DropboxAccountInfo DropboxAccountInfo = null; public string DropboxUploadPath = "Public/ShareX/%y/%mo"; public bool DropboxAutoCreateShareableLink = false; @@ -331,7 +331,7 @@ public bool IsActive(FileDestination destination) switch (destination) { case FileDestination.Dropbox: - return OAuthInfo.CheckOAuth(DropboxOAuthInfo); + return OAuth2Info.CheckOAuth(DropboxOAuth2Info); case FileDestination.Copy: return OAuthInfo.CheckOAuth(CopyOAuthInfo); case FileDestination.GoogleDrive: diff --git a/UploadersLib/UploadersLib.csproj b/UploadersLib/UploadersLib.csproj index 66fcd7751..408e6927a 100644 --- a/UploadersLib/UploadersLib.csproj +++ b/UploadersLib/UploadersLib.csproj @@ -176,6 +176,7 @@ + @@ -221,7 +222,7 @@ - + @@ -351,6 +352,7 @@ +