From c80c71a5cb8d4ed708f2e7f4679194fa96802d7f Mon Sep 17 00:00:00 2001 From: Jaex Date: Wed, 16 Apr 2014 19:18:25 +0300 Subject: [PATCH] Box 2.0 implementation interim commit --- ShareX/UploadTask.cs | 3 +- UploadersLib/FileUploaders/Box.cs | 132 ++++++++++-------- UploadersLib/FileUploaders/Dropbox.cs | 2 +- .../GUI/UploadersConfigForm.Designer.cs | 85 +++++------ UploadersLib/GUI/UploadersConfigForm.cs | 11 +- UploadersLib/GUI/UploadersConfigFormGUI.cs | 7 + UploadersLib/GUI/UploadersConfigFormHelper.cs | 64 +++++++-- UploadersLib/ImageUploaders/Imgur_v3.cs | 2 +- UploadersLib/UploadersConfig.cs | 5 +- 9 files changed, 179 insertions(+), 132 deletions(-) diff --git a/ShareX/UploadTask.cs b/ShareX/UploadTask.cs index 6b72442f9..95505f32e 100644 --- a/ShareX/UploadTask.cs +++ b/ShareX/UploadTask.cs @@ -769,9 +769,8 @@ public UploadResult UploadFile(Stream stream, string fileName) fileUploader = new Minus(Program.UploadersConfig.MinusConfig, Program.UploadersConfig.MinusOAuth2Info); break; case FileDestination.Box: - fileUploader = new Box(APIKeys.BoxKey) + fileUploader = new Box(Program.UploadersConfig.BoxOAuth2Info) { - AuthToken = Program.UploadersConfig.BoxAuthToken, FolderID = Program.UploadersConfig.BoxFolderID, Share = Program.UploadersConfig.BoxShare }; diff --git a/UploadersLib/FileUploaders/Box.cs b/UploadersLib/FileUploaders/Box.cs index 1db0084cd..8a6f17758 100644 --- a/UploadersLib/FileUploaders/Box.cs +++ b/UploadersLib/FileUploaders/Box.cs @@ -24,106 +24,117 @@ You should have received a copy of the GNU General Public License #endregion License Information (GPL v3) using HelpersLib; +using Newtonsoft.Json; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.Xml.Linq; +using UploadersLib.HelperClasses; namespace UploadersLib.FileUploaders { - public sealed class Box : FileUploader + public sealed class Box : FileUploader, IOAuth2 { - private string APIKey; private const string APIURL = "https://www.box.net/api/1.0/rest"; - private const string AuthURL = "https://www.box.net/api/1.0/auth/{0}"; private const string UploadURL = "https://upload.box.net/api/1.0/upload/{0}/{1}"; private const string ShareURL = "http://www.box.com/s/{0}"; - public string Ticket { get; set; } - - public string AuthToken { get; set; } - + public OAuth2Info AuthInfo { get; set; } public string FolderID { get; set; } - public bool Share { get; set; } - public Box(string apiKey) + public Box(OAuth2Info oauth) { - APIKey = apiKey; + AuthInfo = oauth; FolderID = "0"; Share = true; } - public string GetTicket() - { - Dictionary args = new Dictionary(); - args.Add("action", "get_ticket"); - args.Add("api_key", APIKey); - - string response = SendGetRequest(APIURL, args); - - if (!string.IsNullOrEmpty(response)) - { - XDocument xd = XDocument.Parse(response); - XElement xe = xd.GetElement("response"); - - if (xe != null && xe.GetElementValue("status") == "get_ticket_ok") - { - Ticket = xe.GetElementValue("ticket"); - return Ticket; - } - } - - return null; - } - public string GetAuthorizationURL() { - string ticket = GetTicket(); + Dictionary args = new Dictionary(); + args.Add("response_type", "code"); + args.Add("client_id", AuthInfo.Client_ID); - if (!string.IsNullOrEmpty(ticket)) - { - return string.Format(AuthURL, ticket); - } - - return null; + return CreateQuery("https://www.box.com/api/oauth2/authorize", args); } - public string GetAuthToken(string ticket) + public bool GetAccessToken(string pin) { Dictionary args = new Dictionary(); - args.Add("action", "get_auth_token"); - args.Add("api_key", APIKey); - args.Add("ticket", ticket); + args.Add("grant_type", "authorization_code"); + args.Add("code", pin); + args.Add("client_id", AuthInfo.Client_ID); + args.Add("client_secret", AuthInfo.Client_Secret); - string response = SendGetRequest(APIURL, args); + string response = SendPostRequest("https://www.box.com/api/oauth2/token", args); if (!string.IsNullOrEmpty(response)) { - XDocument xd = XDocument.Parse(response); - XElement xe = xd.GetElement("response"); + OAuth2Token token = JsonConvert.DeserializeObject(response); - if (xe != null && xe.GetElementValue("status") == "get_auth_token_ok") + if (token != null && !string.IsNullOrEmpty(token.access_token)) { - AuthToken = xe.GetElementValue("auth_token"); - return AuthToken; + token.UpdateExpireDate(); + AuthInfo.Token = token; + return true; } } - return null; + return false; } - public string GetAuthToken() + public bool RefreshAccessToken() { - return GetAuthToken(Ticket); + if (OAuth2Info.CheckOAuth(AuthInfo) && !string.IsNullOrEmpty(AuthInfo.Token.refresh_token)) + { + Dictionary args = new Dictionary(); + args.Add("grant_type", "refresh_token"); + args.Add("refresh_token", AuthInfo.Token.refresh_token); + args.Add("client_id", AuthInfo.Client_ID); + args.Add("client_secret", AuthInfo.Client_Secret); + + string response = SendPostRequest("https://www.box.com/api/oauth2/token", args); + + if (!string.IsNullOrEmpty(response)) + { + OAuth2Token token = JsonConvert.DeserializeObject(response); + + if (token != null && !string.IsNullOrEmpty(token.access_token)) + { + token.UpdateExpireDate(); + AuthInfo.Token = token; + return true; + } + } + } + + return false; + } + + public bool CheckAuthorization() + { + if (OAuth2Info.CheckOAuth(AuthInfo)) + { + if (AuthInfo.Token.IsExpired && !RefreshAccessToken()) + { + Errors.Add("Refresh access token failed."); + return false; + } + } + else + { + Errors.Add("Box login is required."); + return false; + } + + return true; } public BoxFolder GetAccountTree(string folderID = "0", bool onelevel = false, bool nofiles = false, bool nozip = true, bool simple = false) { NameValueCollection args = new NameValueCollection(); args.Add("action", "get_account_tree"); - args.Add("api_key", APIKey); - args.Add("auth_token", AuthToken); args.Add("folder_id", folderID); if (onelevel) // Make a tree of one level depth, so you will get only the files and folders stored in the folder of the folder_id you have provided. @@ -205,17 +216,18 @@ public BoxFolder GetFolderList() public override UploadResult Upload(Stream stream, string fileName) { - if (string.IsNullOrEmpty(AuthToken)) + if (!CheckAuthorization()) { - Errors.Add("Login is required."); return null; } Dictionary args = new Dictionary(); - if (Share) args.Add("share", "1"); + args.Add("parent_id", FolderID); - string url = string.Format(UploadURL, AuthToken, FolderID); - UploadResult result = UploadData(stream, url, fileName, "file", args); + 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); if (result.IsSuccess) { diff --git a/UploadersLib/FileUploaders/Dropbox.cs b/UploadersLib/FileUploaders/Dropbox.cs index 641284ed5..0c79e89b6 100644 --- a/UploadersLib/FileUploaders/Dropbox.cs +++ b/UploadersLib/FileUploaders/Dropbox.cs @@ -135,7 +135,7 @@ public UploadResult UploadFile(Stream stream, string path, string fileName, bool { if (!OAuthInfo.CheckOAuth(AuthInfo)) { - Errors.Add("Login is required."); + Errors.Add("Dropbox login is required."); return null; } diff --git a/UploadersLib/GUI/UploadersConfigForm.Designer.cs b/UploadersLib/GUI/UploadersConfigForm.Designer.cs index b7a1c68e8..985be63b9 100644 --- a/UploadersLib/GUI/UploadersConfigForm.Designer.cs +++ b/UploadersLib/GUI/UploadersConfigForm.Designer.cs @@ -160,6 +160,7 @@ private void InitializeComponent() this.btnMegaLogin = new System.Windows.Forms.Button(); this.lblMegaStatusTitle = new System.Windows.Forms.Label(); this.tpAmazonS3 = new System.Windows.Forms.TabPage(); + this.btnAmazonS3BucketNameOpen = new System.Windows.Forms.Button(); this.btnAmazonS3AccessKeyOpen = new System.Windows.Forms.Button(); this.cbAmazonS3CustomCNAME = new System.Windows.Forms.CheckBox(); this.cbAmazonS3Endpoint = new System.Windows.Forms.ComboBox(); @@ -186,8 +187,6 @@ private void InitializeComponent() this.lblBoxFolderID = new System.Windows.Forms.Label(); this.btnBoxRefreshFolders = new System.Windows.Forms.Button(); this.tvBoxFolders = new System.Windows.Forms.TreeView(); - this.btnBoxCompleteAuth = new System.Windows.Forms.Button(); - this.btnBoxOpenAuthorize = new System.Windows.Forms.Button(); this.tpRapidShare = new System.Windows.Forms.TabPage(); this.txtRapidShareFolderID = new System.Windows.Forms.TextBox(); this.lblRapidShareFolderID = new System.Windows.Forms.Label(); @@ -349,6 +348,7 @@ private void InitializeComponent() this.atcGistAccountType = new UploadersLib.GUI.AccountTypeControl(); this.ucFTPAccounts = new UploadersLib.AccountsControl(); this.oauth2GoogleDrive = new UploadersLib.GUI.OAuth2Control(); + this.oauth2Box = new UploadersLib.GUI.OAuth2Control(); this.atcSendSpaceAccountType = new UploadersLib.GUI.AccountTypeControl(); this.oAuthJira = new UploadersLib.GUI.OAuth2Control(); this.ucLocalhostAccounts = new UploadersLib.AccountsControl(); @@ -357,7 +357,6 @@ private void InitializeComponent() this.atcGoogleURLShortenerAccountType = new UploadersLib.GUI.AccountTypeControl(); this.ucTwitterAccounts = new UploadersLib.AccountsControl(); this.actRapidShareAccountType = new UploadersLib.GUI.AccountTypeControl(); - this.btnAmazonS3BucketNameOpen = new System.Windows.Forms.Button(); this.tcUploaders.SuspendLayout(); this.tpImageUploaders.SuspendLayout(); this.tcImageUploaders.SuspendLayout(); @@ -1544,7 +1543,7 @@ private void InitializeComponent() this.tlpFtp.RowCount = 2; this.tlpFtp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 75F)); this.tlpFtp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 25F)); - this.tlpFtp.Size = new System.Drawing.Size(798, 451); + this.tlpFtp.Size = new System.Drawing.Size(798, 469); this.tlpFtp.TabIndex = 0; // // panelFtp @@ -1556,7 +1555,7 @@ private void InitializeComponent() this.panelFtp.Dock = System.Windows.Forms.DockStyle.Fill; this.panelFtp.Location = new System.Drawing.Point(3, 3); this.panelFtp.Name = "panelFtp"; - this.panelFtp.Size = new System.Drawing.Size(792, 332); + this.panelFtp.Size = new System.Drawing.Size(792, 345); this.panelFtp.TabIndex = 0; // // btnFtpClient @@ -1606,9 +1605,9 @@ private void InitializeComponent() this.gbFtpSettings.Controls.Add(this.cboFtpText); this.gbFtpSettings.Controls.Add(this.cboFtpImages); this.gbFtpSettings.Dock = System.Windows.Forms.DockStyle.Fill; - this.gbFtpSettings.Location = new System.Drawing.Point(3, 341); + this.gbFtpSettings.Location = new System.Drawing.Point(3, 354); this.gbFtpSettings.Name = "gbFtpSettings"; - this.gbFtpSettings.Size = new System.Drawing.Size(792, 107); + this.gbFtpSettings.Size = new System.Drawing.Size(792, 112); this.gbFtpSettings.TabIndex = 1; this.gbFtpSettings.TabStop = false; this.gbFtpSettings.Text = "FTP Settings"; @@ -1824,6 +1823,16 @@ private void InitializeComponent() this.tpAmazonS3.Text = "Amazon S3"; this.tpAmazonS3.UseVisualStyleBackColor = true; // + // btnAmazonS3BucketNameOpen + // + this.btnAmazonS3BucketNameOpen.Location = new System.Drawing.Point(464, 104); + this.btnAmazonS3BucketNameOpen.Name = "btnAmazonS3BucketNameOpen"; + this.btnAmazonS3BucketNameOpen.Size = new System.Drawing.Size(24, 24); + this.btnAmazonS3BucketNameOpen.TabIndex = 22; + this.btnAmazonS3BucketNameOpen.Text = "?"; + this.btnAmazonS3BucketNameOpen.UseVisualStyleBackColor = true; + this.btnAmazonS3BucketNameOpen.Click += new System.EventHandler(this.btnAmazonS3BucketNameOpen_Click); + // // btnAmazonS3AccessKeyOpen // this.btnAmazonS3AccessKeyOpen.Location = new System.Drawing.Point(464, 21); @@ -2047,12 +2056,11 @@ private void InitializeComponent() // // tpBox // + this.tpBox.Controls.Add(this.oauth2Box); this.tpBox.Controls.Add(this.txtBoxFolderID); this.tpBox.Controls.Add(this.lblBoxFolderID); this.tpBox.Controls.Add(this.btnBoxRefreshFolders); this.tpBox.Controls.Add(this.tvBoxFolders); - this.tpBox.Controls.Add(this.btnBoxCompleteAuth); - this.tpBox.Controls.Add(this.btnBoxOpenAuthorize); this.tpBox.Location = new System.Drawing.Point(4, 40); this.tpBox.Name = "tpBox"; this.tpBox.Padding = new System.Windows.Forms.Padding(3); @@ -2063,7 +2071,7 @@ private void InitializeComponent() // // txtBoxFolderID // - this.txtBoxFolderID.Location = new System.Drawing.Point(128, 52); + this.txtBoxFolderID.Location = new System.Drawing.Point(464, 20); this.txtBoxFolderID.Name = "txtBoxFolderID"; this.txtBoxFolderID.Size = new System.Drawing.Size(88, 20); this.txtBoxFolderID.TabIndex = 3; @@ -2072,7 +2080,7 @@ private void InitializeComponent() // lblBoxFolderID // this.lblBoxFolderID.AutoSize = true; - this.lblBoxFolderID.Location = new System.Drawing.Point(16, 56); + this.lblBoxFolderID.Location = new System.Drawing.Point(352, 24); this.lblBoxFolderID.Name = "lblBoxFolderID"; this.lblBoxFolderID.Size = new System.Drawing.Size(103, 13); this.lblBoxFolderID.TabIndex = 2; @@ -2080,7 +2088,8 @@ private void InitializeComponent() // // btnBoxRefreshFolders // - this.btnBoxRefreshFolders.Location = new System.Drawing.Point(16, 80); + this.btnBoxRefreshFolders.Enabled = false; + this.btnBoxRefreshFolders.Location = new System.Drawing.Point(352, 48); this.btnBoxRefreshFolders.Name = "btnBoxRefreshFolders"; this.btnBoxRefreshFolders.Size = new System.Drawing.Size(128, 23); this.btnBoxRefreshFolders.TabIndex = 4; @@ -2090,33 +2099,12 @@ private void InitializeComponent() // // tvBoxFolders // - this.tvBoxFolders.Location = new System.Drawing.Point(16, 112); + this.tvBoxFolders.Location = new System.Drawing.Point(352, 80); this.tvBoxFolders.Name = "tvBoxFolders"; - this.tvBoxFolders.Size = new System.Drawing.Size(440, 312); + this.tvBoxFolders.Size = new System.Drawing.Size(440, 360); this.tvBoxFolders.TabIndex = 5; this.tvBoxFolders.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.tvBoxFolders_AfterSelect); // - // btnBoxCompleteAuth - // - this.btnBoxCompleteAuth.Enabled = false; - this.btnBoxCompleteAuth.Location = new System.Drawing.Point(192, 16); - this.btnBoxCompleteAuth.Name = "btnBoxCompleteAuth"; - this.btnBoxCompleteAuth.Size = new System.Drawing.Size(168, 23); - this.btnBoxCompleteAuth.TabIndex = 1; - this.btnBoxCompleteAuth.Text = "2. Complete authorization"; - this.btnBoxCompleteAuth.UseVisualStyleBackColor = true; - this.btnBoxCompleteAuth.Click += new System.EventHandler(this.btnBoxCompleteAuth_Click); - // - // btnBoxOpenAuthorize - // - this.btnBoxOpenAuthorize.Location = new System.Drawing.Point(16, 16); - this.btnBoxOpenAuthorize.Name = "btnBoxOpenAuthorize"; - this.btnBoxOpenAuthorize.Size = new System.Drawing.Size(168, 23); - this.btnBoxOpenAuthorize.TabIndex = 0; - this.btnBoxOpenAuthorize.Text = "1. Open authorize page..."; - this.btnBoxOpenAuthorize.UseVisualStyleBackColor = true; - this.btnBoxOpenAuthorize.Click += new System.EventHandler(this.btnBoxOpenAuthorize_Click); - // // tpRapidShare // this.tpRapidShare.Controls.Add(this.txtRapidShareFolderID); @@ -3765,7 +3753,7 @@ private void InitializeComponent() this.ucFTPAccounts.Location = new System.Drawing.Point(0, 0); this.ucFTPAccounts.Margin = new System.Windows.Forms.Padding(4); this.ucFTPAccounts.Name = "ucFTPAccounts"; - this.ucFTPAccounts.Size = new System.Drawing.Size(792, 332); + this.ucFTPAccounts.Size = new System.Drawing.Size(792, 345); this.ucFTPAccounts.TabIndex = 0; // // oauth2GoogleDrive @@ -3780,6 +3768,18 @@ private void InitializeComponent() this.oauth2GoogleDrive.CompleteButtonClicked += new UploadersLib.GUI.OAuth2Control.CompleteButtonClickedEventHandler(this.oauth2GoogleDrive_CompleteButtonClicked); this.oauth2GoogleDrive.RefreshButtonClicked += new UploadersLib.GUI.OAuth2Control.RefreshButtonClickedEventHandler(this.oauth2GoogleDrive_RefreshButtonClicked); // + // oauth2Box + // + this.oauth2Box.Location = new System.Drawing.Point(16, 16); + this.oauth2Box.LoginStatus = false; + this.oauth2Box.Name = "oauth2Box"; + this.oauth2Box.Size = new System.Drawing.Size(328, 207); + this.oauth2Box.Status = "Status: Login required."; + this.oauth2Box.TabIndex = 7; + this.oauth2Box.OpenButtonClicked += new UploadersLib.GUI.OAuth2Control.OpenButtonClickedEventHandler(this.oauth2Box_OpenButtonClicked); + this.oauth2Box.CompleteButtonClicked += new UploadersLib.GUI.OAuth2Control.CompleteButtonClickedEventHandler(this.oauth2Box_CompleteButtonClicked); + this.oauth2Box.RefreshButtonClicked += new UploadersLib.GUI.OAuth2Control.RefreshButtonClickedEventHandler(this.oauth2Box_RefreshButtonClicked); + // // atcSendSpaceAccountType // this.atcSendSpaceAccountType.Location = new System.Drawing.Point(8, 16); @@ -3859,16 +3859,6 @@ private void InitializeComponent() this.actRapidShareAccountType.Size = new System.Drawing.Size(214, 29); this.actRapidShareAccountType.TabIndex = 16; // - // btnAmazonS3BucketNameOpen - // - this.btnAmazonS3BucketNameOpen.Location = new System.Drawing.Point(464, 104); - this.btnAmazonS3BucketNameOpen.Name = "btnAmazonS3BucketNameOpen"; - this.btnAmazonS3BucketNameOpen.Size = new System.Drawing.Size(24, 24); - this.btnAmazonS3BucketNameOpen.TabIndex = 22; - this.btnAmazonS3BucketNameOpen.Text = "?"; - this.btnAmazonS3BucketNameOpen.UseVisualStyleBackColor = true; - this.btnAmazonS3BucketNameOpen.Click += new System.EventHandler(this.btnAmazonS3BucketNameOpen_Click); - // // UploadersConfigForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -4176,8 +4166,6 @@ private void InitializeComponent() private System.Windows.Forms.TextBox txtRapidShareFolderID; private System.Windows.Forms.Label lblRapidShareFolderID; private System.Windows.Forms.TabPage tpBox; - private System.Windows.Forms.Button btnBoxCompleteAuth; - private System.Windows.Forms.Button btnBoxOpenAuthorize; private System.Windows.Forms.TextBox txtBoxFolderID; private System.Windows.Forms.Label lblBoxFolderID; private System.Windows.Forms.Button btnBoxRefreshFolders; @@ -4317,5 +4305,6 @@ private void InitializeComponent() private System.Windows.Forms.TextBox txtPushbulletUserKey; private System.Windows.Forms.Button btnAmazonS3AccessKeyOpen; private System.Windows.Forms.Button btnAmazonS3BucketNameOpen; + private GUI.OAuth2Control oauth2Box; } } \ No newline at end of file diff --git a/UploadersLib/GUI/UploadersConfigForm.cs b/UploadersLib/GUI/UploadersConfigForm.cs index da5eaf264..ed7bfcb75 100644 --- a/UploadersLib/GUI/UploadersConfigForm.cs +++ b/UploadersLib/GUI/UploadersConfigForm.cs @@ -514,14 +514,19 @@ private void cbGoogleDriveIsPublic_CheckedChanged(object sender, EventArgs e) #region Box - private void btnBoxOpenAuthorize_Click(object sender, EventArgs e) + private void oauth2Box_OpenButtonClicked() { BoxAuthOpen(); } - private void btnBoxCompleteAuth_Click(object sender, EventArgs e) + private void oauth2Box_CompleteButtonClicked(string code) { - BoxAuthComplete(); + BoxAuthComplete(code); + } + + private void oauth2Box_RefreshButtonClicked() + { + BoxAuthRefresh(); } private void txtBoxFolderID_TextChanged(object sender, EventArgs e) diff --git a/UploadersLib/GUI/UploadersConfigFormGUI.cs b/UploadersLib/GUI/UploadersConfigFormGUI.cs index d9f3d01f6..ec51cbaab 100644 --- a/UploadersLib/GUI/UploadersConfigFormGUI.cs +++ b/UploadersLib/GUI/UploadersConfigFormGUI.cs @@ -288,6 +288,13 @@ public void LoadSettings(UploadersConfig uploadersConfig) // Box + if (OAuth2Info.CheckOAuth(Config.BoxOAuth2Info)) + { + oauth2Box.Status = "Login successful."; + oauth2Box.LoginStatus = true; + btnBoxRefreshFolders.Enabled = true; + } + txtBoxFolderID.Text = Config.BoxFolderID; // Ge.tt diff --git a/UploadersLib/GUI/UploadersConfigFormHelper.cs b/UploadersLib/GUI/UploadersConfigFormHelper.cs index b7c210005..34fd0b96b 100644 --- a/UploadersLib/GUI/UploadersConfigFormHelper.cs +++ b/UploadersLib/GUI/UploadersConfigFormHelper.cs @@ -636,15 +636,19 @@ public void BoxAuthOpen() { try { - Box box = new Box(APIKeys.BoxKey); + OAuth2Info oauth = new OAuth2Info(APIKeys.BoxClientID, APIKeys.BoxClientSecret); - string url = box.GetAuthorizationURL(); + string url = new Box(oauth).GetAuthorizationURL(); if (!string.IsNullOrEmpty(url)) { - Config.BoxTicket = box.Ticket; + Config.BoxOAuth2Info = oauth; Helpers.LoadBrowserAsync(url); - btnBoxCompleteAuth.Enabled = true; + DebugHelper.WriteLine("BoxAuthOpen - Authorization URL is opened: " + url); + } + else + { + DebugHelper.WriteLine("BoxAuthOpen - Authorization URL is empty."); } } catch (Exception ex) @@ -653,41 +657,73 @@ public void BoxAuthOpen() } } - public void BoxAuthComplete() + public void BoxAuthComplete(string code) { - if (!string.IsNullOrEmpty(Config.BoxTicket)) + try { - try + if (!string.IsNullOrEmpty(code) && Config.BoxOAuth2Info != null) { - Box box = new Box(APIKeys.BoxKey) { Ticket = Config.BoxTicket }; - Config.BoxAuthToken = box.GetAuthToken(); + bool result = new Box(Config.BoxOAuth2Info).GetAccessToken(code); - if (!string.IsNullOrEmpty(Config.BoxAuthToken)) + if (result) { + oauth2Box.Status = "Login successful."; MessageBox.Show("Login successful.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); } else { + oauth2Box.Status = "Login failed."; MessageBox.Show("Login failed.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); } + + oauth2Box.LoginStatus = result; + btnBoxRefreshFolders.Enabled = result; } - catch (Exception ex) + } + catch (Exception ex) + { + MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + public void BoxAuthRefresh() + { + try + { + if (OAuth2Info.CheckOAuth(Config.BoxOAuth2Info)) { - MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + bool result = new Box(Config.BoxOAuth2Info).RefreshAccessToken(); + + if (result) + { + oauth2Box.Status = "Login successful."; + MessageBox.Show("Login successful.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + oauth2Box.Status = "Login failed."; + MessageBox.Show("Login failed.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); + } + + btnBoxRefreshFolders.Enabled = result; } } + catch (Exception ex) + { + MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } public void BoxListFolders() { - if (string.IsNullOrEmpty(Config.BoxAuthToken)) + if (!OAuth2Info.CheckOAuth(Config.BoxOAuth2Info)) { MessageBox.Show("Authentication required.", "Box refresh folders list failed", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { tvBoxFolders.Nodes.Clear(); - Box box = new Box(APIKeys.BoxKey) { AuthToken = Config.BoxAuthToken }; + Box box = new Box(Config.BoxOAuth2Info); BoxFolder root = box.GetFolderList(); BoxRecursiveAddChilds(tvBoxFolders.Nodes, root); tvBoxFolders.ExpandAll(); diff --git a/UploadersLib/ImageUploaders/Imgur_v3.cs b/UploadersLib/ImageUploaders/Imgur_v3.cs index 0d273a72b..046f4caf0 100644 --- a/UploadersLib/ImageUploaders/Imgur_v3.cs +++ b/UploadersLib/ImageUploaders/Imgur_v3.cs @@ -117,7 +117,7 @@ public bool CheckAuthorization() } else { - Errors.Add("Login is required."); + Errors.Add("Imgur login is required."); return false; } diff --git a/UploadersLib/UploadersConfig.cs b/UploadersLib/UploadersConfig.cs index afaf84293..0e2595354 100644 --- a/UploadersLib/UploadersConfig.cs +++ b/UploadersLib/UploadersConfig.cs @@ -146,8 +146,7 @@ public class UploadersConfig : SettingsBase // Box - public string BoxTicket = string.Empty; - public string BoxAuthToken = string.Empty; + public OAuth2Info BoxOAuth2Info = null; public string BoxFolderID = "0"; public bool BoxShare = true; @@ -336,7 +335,7 @@ public bool IsActive(FileDestination destination) case FileDestination.Minus: return MinusConfig != null && MinusConfig.MinusUser != null; case FileDestination.Box: - return !string.IsNullOrEmpty(BoxAuthToken); + return OAuth2Info.CheckOAuth(BoxOAuth2Info); case FileDestination.Ge_tt: return Ge_ttLogin != null && !string.IsNullOrEmpty(Ge_ttLogin.AccessToken); case FileDestination.Localhostr: