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

时间:2012-12-15 17:25:28

标签: c# dropbox

我目前正在开发一个c#项目,我正在尝试在我的程序中实现dropbox。我已成功设法验证Dropbox并创建一个目录,但我无法弄清楚如何上传文件。

Dropbox访问类型是AppFolder,我可以在app文件夹中创建一个文件夹。要上传文件,只是为了测试,我正在写一些文本字段并提交表单,然后我将字符串写入文本文件,在可执行文件的工作目录中,然后我尝试将文件上传到app文件夹的根目录,但是抛出异常one more errors have occurred可能是最不实用的错误。

以下是重要的是我如何验证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);
            }
        }

下面是关于我如何创建目录的代码,该目录正在运行,然后上传文件

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);
            }
        }

我不确定uploadFileEntry是否是我想要的那个,因为我不明白为什么我需要说新的AssemblyResource因为它不是程序的资源,它只是一个文本文件创建

感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:1)

我已经设法找到了在经过数小时拖网浏览谷歌后该做些什么,设法找到了一些运气。

它不需要是一个程序集资源,所以我改为创建该文件,但在上传之前我执行以下操作:

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

然后将上传更改为以下内容:

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

注意,我不再做新的AssemblyResource了。