I am currently working on a c# project and I am trying to implement dropbox within my program. 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. 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.
In case it matters below is how I am authenticating 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.
Thanks for any help you can provide.