简体   繁体   English

使用Spring.Social Dropbox库将文件上传到Dropbox

[英]Uploading file to Dropbox using Spring.Social Dropbox library

I am currently working on ac# project and I am trying to implement dropbox within my program. 我目前正在从事ac#项目,并且正在尝试在程序中实现保管箱。 I have successfully managed to authenticate dropbox and create a directory but I can't work out how to upload a file. 我已经成功地完成了对保管箱的身份验证并创建了目录,但是我不知道如何上传文件。

The Dropbox access type is AppFolder and I can create a folder within the app folder. Dropbox访问类型为AppFolder,我可以在app文件夹中创建一个文件夹。 To upload the file, just for a test, I am writing something to a text field and submitting the form, I then write the string to a text file, within the working directory of the executable and I am then trying to upload the file into the root of the app folder, but it is throwing an exception one more errors have occurred probably the least useful error. 要上传文件,只是为了进行测试,我正在向文本字段中写入内容并提交表单,然后将字符串写入可执行文件的工作目录中的文本文件中,然后尝试将文件上传至app文件夹的根目录,但它引发异常,可能是最不有用的错误了,又one more errors have occurredone more errors have occurred错误。

In case it matters below is how I am authenticating Dropbox. 以防万一,下面是我如何验证Dropbox。

private void authenticateDropbox(Boolean isFromCallBack = false)
        {
            try
            {
                if (!isFromCallBack)
                {
                    dropboxServiceProvider = new DropboxServiceProvider(dropboxAppKey, dropboxAppSecret, AccessLevel.AppFolder);
                    //lblStatus.Content = "Getting request Token";

                    oauthToken = dropboxServiceProvider.OAuthOperations.FetchRequestTokenAsync(callbackUrl, null).Result;
                    //lblStatus.Content = "Request token retrieved";

                    parameters = new OAuth1Parameters();
                    parameters.CallbackUrl = callbackUrl;
                    string authenticateUrl = dropboxServiceProvider.OAuthOperations.BuildAuthorizeUrl(oauthToken.Value, parameters);
                    webDropbox.Navigate(new Uri(authenticateUrl));
                }
                else
                {
                    AuthorizedRequestToken requestToken = new AuthorizedRequestToken(oauthToken, null);
                    OAuthToken oauthAccessToken = dropboxServiceProvider.OAuthOperations.ExchangeForAccessTokenAsync(requestToken, null).Result;

                    Properties.Settings.Default.dropbox_accessSecret = oauthAccessToken.Secret;
                    Properties.Settings.Default.dropbox_accessToken = oauthAccessToken.Value;
                    Properties.Settings.Default.Save();

                    IDropbox dropbox = dropboxServiceProvider.GetApi(oauthAccessToken.Value, oauthAccessToken.Secret);

                    DropboxProfile profile = dropbox.GetUserProfileAsync().Result;
                    Close();
                    MessageBox.Show("Welcome " + profile.DisplayName, "DropBox Success", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
            catch (AggregateException ex)
            {
                MessageBox.Show("AggregateException: Failed to authenticate\n\n" + ex.Message, "Authentication Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show("General Exception: Failed to authenticate\n\n" + ex.Message, "Authentication Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void webDropbox_Navigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            string url = e.Uri.ToString();
            if (url.StartsWith(callbackUrl, StringComparison.OrdinalIgnoreCase))
            {
                authenticateDropbox(true);
            }
        }

Below is the code on how I am creating the directory, which is working, then upload a file 以下是有关如何创建目录的代码,该目录正在工作,然后上传文件

private void uploadContentToDropbox()
        {
            try
            {
                DropboxServiceProvider dropboxServiceProvider = new DropboxServiceProvider(dropboxAppKey, dropboxAppSecret, AccessLevel.AppFolder);
                IDropbox dropbox = dropboxServiceProvider.GetApi(Properties.Settings.Default.dropbox_accessToken, Properties.Settings.Default.dropbox_accessSecret);

                //DeltaPage deltaPage = dropbox.DeltaAsync(null).Result;
                writeContentToFile();

                Entry createFolderEntry = dropbox.CreateFolderAsync("Test Folder").Result;

                Entry uploadFileEntry = dropbox.UploadFileAsync(
                    new AssemblyResource("assembly://DropBoxTest/DropBoxTest/upload.txt"), "upload.txt", true, null, CancellationToken.None).Result;
            }
            catch (AggregateException ex)
            {
                MessageBox.Show("Failed to upload:\n\n" + ex.Message, "Dropbox Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show("General failed to upload:\n\n" + ex.Message, "Dropbox Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

I'm not sure if the uploadFileEntry is the one that I want as I don't see why I need to say new AssemblyResource as its not a resource of the program it is just a text file that is created. 我不确定uploadFileEntry是否是我想要的文件,因为我不明白为什么我需要说新的AssemblyResource,因为它不是程序的资源,而只是创建的文本文件。

Thanks for any help you can provide. 感谢您的任何帮助,您可以提供。

I've managed to find out what to do after hours of trawling through Google, managed to get some luck to find something. 经过数小时的Google搜寻后,我设法找到了解决方法,并找到了一些运气。

It doesn't need to be an assembly resource, so I instead create the file as I was before but before doing the upload I do the following: 它不需要是程序集资源,因此我像以前一样创建文件,但是在执行上传之前,请执行以下操作:

var res = new FileResource("upload.txt");

And then change the upload to the following: 然后将上传更改为以下内容:

Entry uploadFileEntry = dropbox.UploadFileAsync(
                    res, "upload.txt", true, null, CancellationToken.None).Result;

Notice, I'm not doing new AssemblyResource anymore. 注意,我不再使用新的AssemblyResource。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM