Dropbox OAuth and get account info works now

This commit is contained in:
Jaex 2016-06-29 03:52:45 +03:00
parent 68509503a3
commit d0055bb819
5 changed files with 70 additions and 37 deletions

View file

@ -49,7 +49,7 @@ public override bool CheckConfig(UploadersConfig config)
public override GenericUploader CreateUploader(UploadersConfig config, TaskReferenceHelper taskInfo) public override GenericUploader CreateUploader(UploadersConfig config, TaskReferenceHelper taskInfo)
{ {
return new Dropbox(config.DropboxOAuth2Info, config.DropboxAccountInfo) return new Dropbox(config.DropboxOAuth2Info, config.DropboxAccount)
{ {
UploadPath = NameParser.Parse(NameParserType.URL, Dropbox.TidyUploadPath(config.DropboxUploadPath)), UploadPath = NameParser.Parse(NameParserType.URL, Dropbox.TidyUploadPath(config.DropboxUploadPath)),
AutoCreateShareableLink = config.DropboxAutoCreateShareableLink, AutoCreateShareableLink = config.DropboxAutoCreateShareableLink,
@ -63,7 +63,7 @@ public override GenericUploader CreateUploader(UploadersConfig config, TaskRefer
public sealed class Dropbox : FileUploader, IOAuth2Basic public sealed class Dropbox : FileUploader, IOAuth2Basic
{ {
public OAuth2Info AuthInfo { get; set; } public OAuth2Info AuthInfo { get; set; }
public DropboxAccountInfo AccountInfo { get; set; } public DropboxAccount Account { get; set; }
public string UploadPath { get; set; } public string UploadPath { get; set; }
public bool AutoCreateShareableLink { get; set; } public bool AutoCreateShareableLink { get; set; }
public DropboxURLType ShareURLType { get; set; } public DropboxURLType ShareURLType { get; set; }
@ -71,12 +71,14 @@ public sealed class Dropbox : FileUploader, IOAuth2Basic
private const string APIVersion = "2"; private const string APIVersion = "2";
private const string Root = "dropbox"; private const string Root = "dropbox";
private const string URLWEB = "https://www.dropbox.com/"; private const string URLWEB = "https://www.dropbox.com";
private const string URLAPI = "https://api.dropboxapi.com/" + APIVersion; private const string URLAPIBase = "https://api.dropboxapi.com";
private const string URLAPI = URLAPIBase + "/" + APIVersion;
private const string URLContent = "https://content.dropboxapi.com/" + APIVersion; private const string URLContent = "https://content.dropboxapi.com/" + APIVersion;
private const string URLNotify = "https://notify.dropboxapi.com/" + APIVersion;
private const string URLOAuth2Authorize = URLWEB + "/oauth2/authorize"; private const string URLOAuth2Authorize = URLWEB + "/oauth2/authorize";
private const string URLOAuth2Token = URLAPI + "/oauth2/token"; private const string URLOAuth2Token = URLAPIBase + "/oauth2/token";
private const string URLGetCurrentAccount = URLAPI + "/users/get_current_account"; private const string URLGetCurrentAccount = URLAPI + "/users/get_current_account";
private const string URLDownload = URLContent + "/files/download"; private const string URLDownload = URLContent + "/files/download";
@ -96,9 +98,9 @@ public Dropbox(OAuth2Info oauth)
AuthInfo = oauth; AuthInfo = oauth;
} }
public Dropbox(OAuth2Info oauth, DropboxAccountInfo accountInfo) : this(oauth) public Dropbox(OAuth2Info oauth, DropboxAccount account) : this(oauth)
{ {
AccountInfo = accountInfo; Account = account;
} }
public string GetAuthorizationURL() public string GetAuthorizationURL()
@ -143,21 +145,21 @@ private NameValueCollection GetAuthHeaders()
#region Dropbox accounts #region Dropbox accounts
public DropboxAccountInfo GetAccountInfo() public DropboxAccount GetAccountInfo()
{ {
DropboxAccountInfo account = null; DropboxAccount account = null;
if (OAuth2Info.CheckOAuth(AuthInfo)) if (OAuth2Info.CheckOAuth(AuthInfo))
{ {
string response = SendRequest(HttpMethod.GET, URLGetCurrentAccount, headers: GetAuthHeaders()); string response = SendRequestJSON(URLGetCurrentAccount, "null", GetAuthHeaders());
if (!string.IsNullOrEmpty(response)) if (!string.IsNullOrEmpty(response))
{ {
account = JsonConvert.DeserializeObject<DropboxAccountInfo>(response); account = JsonConvert.DeserializeObject<DropboxAccount>(response);
if (account != null) if (account != null)
{ {
AccountInfo = account; Account = account;
} }
} }
} }
@ -383,10 +385,11 @@ private void CheckEarlyURLCopy(string path, string fileName)
public string GetPublicURL(string path) public string GetPublicURL(string path)
{ {
return GetPublicURL(AccountInfo.Uid, path); // TODO: uid
return GetPublicURL(Account.account_id, path);
} }
public static string GetPublicURL(long userID, string path) public static string GetPublicURL(string userID, string path)
{ {
if (!string.IsNullOrEmpty(path)) if (!string.IsNullOrEmpty(path))
{ {
@ -395,7 +398,7 @@ public static string GetPublicURL(long userID, string path)
if (path.StartsWith("Public/", StringComparison.InvariantCultureIgnoreCase)) if (path.StartsWith("Public/", StringComparison.InvariantCultureIgnoreCase))
{ {
path = URLHelpers.URLPathEncode(path.Substring(7)); path = URLHelpers.URLPathEncode(path.Substring(7));
return URLHelpers.CombineURL(URLPublicDirect, userID.ToString(), path); return URLHelpers.CombineURL(URLPublicDirect, userID, path);
} }
} }
@ -420,14 +423,42 @@ public enum DropboxURLType
Direct Direct
} }
public class DropboxAccountInfo public class DropboxPath
{ {
public string Referral_link { get; set; } // The user's referral link. public string path { get; set; }
public string Display_name { get; set; } // The user's display name.
public long Uid { get; set; } // The user's unique Dropbox ID. public DropboxPath(string path)
public string Country { get; set; } // The user's two-letter country code, if available. {
public DropboxQuotaInfo Quota_info { get; set; } this.path = path;
public string Email { get; set; } }
}
public class DropboxAccount
{
public string account_id { get; set; }
public DropboxAccountName name { get; set; }
public string email { get; set; }
public bool email_verified { get; set; }
public bool disabled { get; set; }
public string locale { get; set; }
public string referral_link { get; set; }
public bool is_paired { get; set; }
public DropboxAccountType account_type { get; set; }
public string profile_photo_url { get; set; }
public string country { get; set; }
}
public class DropboxAccountName
{
public string given_name { get; set; }
public string surname { get; set; }
public string familiar_name { get; set; }
public string display_name { get; set; }
}
public class DropboxAccountType
{
public string tag { get; set; }
} }
public class DropboxQuotaInfo public class DropboxQuotaInfo

View file

@ -251,7 +251,7 @@ public override UploadResult Upload(Stream stream, string fileName)
string metadata = GetMetadata(fileName, FolderID); string metadata = GetMetadata(fileName, FolderID);
UploadResult result = UploadData(stream, "https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart", fileName, headers: GetAuthHeaders(), UploadResult result = UploadData(stream, "https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart", fileName, headers: GetAuthHeaders(),
requestContentType: "multipart/related", metadata: metadata); contentType: "multipart/related", metadata: metadata);
if (!string.IsNullOrEmpty(result.Response)) if (!string.IsNullOrEmpty(result.Response))
{ {

View file

@ -39,17 +39,17 @@ public partial class DropboxFilesForm : Form
public string CurrentFolderPath { get; private set; } public string CurrentFolderPath { get; private set; }
private Dropbox dropbox; private Dropbox dropbox;
private DropboxAccountInfo dropboxAccountInfo; private DropboxAccount dropboxAccount;
private ImageListManager ilm; private ImageListManager ilm;
private bool isSelectedFile, isSelectedPublic; private bool isSelectedFile, isSelectedPublic;
public DropboxFilesForm(OAuth2Info oauth, string path, DropboxAccountInfo accountInfo) public DropboxFilesForm(OAuth2Info oauth, string path, DropboxAccount account)
{ {
InitializeComponent(); InitializeComponent();
Icon = ShareXResources.Icon; Icon = ShareXResources.Icon;
dropbox = new Dropbox(oauth); dropbox = new Dropbox(oauth);
dropboxAccountInfo = accountInfo; dropboxAccount = account;
ilm = new ImageListManager(lvDropboxFiles); ilm = new ImageListManager(lvDropboxFiles);
if (path != null) if (path != null)
@ -156,7 +156,8 @@ private void tsmiCopyPublicLink_Click(object sender, EventArgs e)
if (content != null && !content.Is_dir && content.Path.StartsWith("/Public/", StringComparison.InvariantCultureIgnoreCase)) if (content != null && !content.Is_dir && content.Path.StartsWith("/Public/", StringComparison.InvariantCultureIgnoreCase))
{ {
string url = Dropbox.GetPublicURL(dropboxAccountInfo.Uid, content.Path); // TODO: uid
string url = Dropbox.GetPublicURL(dropboxAccount.account_id, content.Path);
ClipboardHelpers.CopyText(url); ClipboardHelpers.CopyText(url);
} }
} }

View file

@ -437,7 +437,7 @@ public void DropboxOpenFiles()
{ {
if (OAuth2Info.CheckOAuth(Config.DropboxOAuth2Info)) if (OAuth2Info.CheckOAuth(Config.DropboxOAuth2Info))
{ {
using (DropboxFilesForm filesForm = new DropboxFilesForm(Config.DropboxOAuth2Info, GetDropboxUploadPath(), Config.DropboxAccountInfo)) using (DropboxFilesForm filesForm = new DropboxFilesForm(Config.DropboxOAuth2Info, GetDropboxUploadPath(), Config.DropboxAccount))
{ {
if (filesForm.ShowDialog() == DialogResult.OK) if (filesForm.ShowDialog() == DialogResult.OK)
{ {
@ -483,12 +483,12 @@ public void DropboxAuthComplete(string code)
if (result) if (result)
{ {
Config.DropboxAccountInfo = dropbox.GetAccountInfo(); Config.DropboxAccount = dropbox.GetAccountInfo();
UpdateDropboxStatus(); UpdateDropboxStatus();
oauth2Dropbox.Status = OAuthLoginStatus.LoginSuccessful; oauth2Dropbox.Status = OAuthLoginStatus.LoginSuccessful;
if (Config.DropboxAccountInfo != null) if (Config.DropboxAccount != null)
{ {
MessageBox.Show(Resources.UploadersConfigForm_Login_successful, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBox.Show(Resources.UploadersConfigForm_Login_successful, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Information);
} }
@ -507,7 +507,7 @@ public void DropboxAuthComplete(string code)
} }
} }
Config.DropboxAccountInfo = null; Config.DropboxAccount = null;
UpdateDropboxStatus(); UpdateDropboxStatus();
} }
catch (Exception ex) catch (Exception ex)
@ -519,15 +519,16 @@ public void DropboxAuthComplete(string code)
private void UpdateDropboxStatus() private void UpdateDropboxStatus()
{ {
if (OAuth2Info.CheckOAuth(Config.DropboxOAuth2Info) && Config.DropboxAccountInfo != null) if (OAuth2Info.CheckOAuth(Config.DropboxOAuth2Info) && Config.DropboxAccount != null)
{ {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.AppendLine(Resources.UploadersConfigForm_UpdateDropboxStatus_Email_ + " " + Config.DropboxAccountInfo.Email); sb.AppendLine(Resources.UploadersConfigForm_UpdateDropboxStatus_Email_ + " " + Config.DropboxAccount.email);
sb.AppendLine(Resources.UploadersConfigForm_UpdateDropboxStatus_Name_ + " " + Config.DropboxAccountInfo.Display_name); sb.AppendLine(Resources.UploadersConfigForm_UpdateDropboxStatus_Name_ + " " + Config.DropboxAccount.name.display_name);
sb.AppendLine(Resources.UploadersConfigForm_UpdateDropboxStatus_User_ID_ + " " + Config.DropboxAccountInfo.Uid.ToString()); sb.AppendLine(Resources.UploadersConfigForm_UpdateDropboxStatus_User_ID_ + " " + Config.DropboxAccount.account_id);
string uploadPath = GetDropboxUploadPath(); string uploadPath = GetDropboxUploadPath();
sb.AppendLine(Resources.UploadersConfigForm_UpdateDropboxStatus_Upload_path_ + " " + uploadPath); sb.AppendLine(Resources.UploadersConfigForm_UpdateDropboxStatus_Upload_path_ + " " + uploadPath);
sb.AppendLine(Resources.UploadersConfigForm_UpdateDropboxStatus_Download_path_ + " " + Dropbox.GetPublicURL(Config.DropboxAccountInfo.Uid, uploadPath + "Example.png")); // TODO: uid
sb.AppendLine(Resources.UploadersConfigForm_UpdateDropboxStatus_Download_path_ + " " + Dropbox.GetPublicURL(Config.DropboxAccount.account_id, uploadPath + "Example.png"));
lblDropboxStatus.Text = sb.ToString(); lblDropboxStatus.Text = sb.ToString();
btnDropboxShowFiles.Enabled = true; btnDropboxShowFiles.Enabled = true;
} }

View file

@ -130,7 +130,7 @@ public class UploadersConfig : SettingsBase<UploadersConfig>
// Dropbox // Dropbox
public OAuth2Info DropboxOAuth2Info = null; public OAuth2Info DropboxOAuth2Info = null;
public DropboxAccountInfo DropboxAccountInfo = null; public DropboxAccount DropboxAccount = null;
public string DropboxUploadPath = "Public/ShareX/%y/%mo"; public string DropboxUploadPath = "Public/ShareX/%y/%mo";
public bool DropboxAutoCreateShareableLink = false; public bool DropboxAutoCreateShareableLink = false;
public DropboxURLType DropboxURLType = DropboxURLType.Default; public DropboxURLType DropboxURLType = DropboxURLType.Default;