Dropbox.NET
Dropbox API is a .NET SDK for API v2, which helps you easily integrate Dropbox into your app. The Dropbox .NET SDK is a Portable Class Library that works with multiple platforms, including Windows, Windows Phone, and Mono.
Step 1 Create a WPF Application
- Create a new WPF application.
Step 2 Install Dropbox.NET SDK in to the project
- Dropbox.NET SDK can be installed through NuGet.
- Open NuGet Package Manager Console (PMC) in Visual Studio.
- Run the below-mentioned command in PMC to install Dropbox API.
PM> Install-Package Dropbox.Api
- Follow the basic steps to complete the installation.
- After successful installation, Dropbox.Api will be added into the project references.
Step 3 Create App in Dropbox
- Go to the below-mentioned link to create an account in Dropbox.
https://www.dropbox.com
- Navigate to below-mentioned link to create new app required to work with Dropbox API.
https://www.dropbox.com/developers/apps
- Follow the steps and create a new app as per your convenience.
Step 4 Configure Created App for Access
- Click on the created application and redirect to the Settings tab.
- Initially, only one user can access API and to enable multiple users, click on “Enable additional users” button. This will allow 500 users to use this app for API use.
- App Key: This is a unique key generated for this app which will be required for the API connection.
- Redirected URIs: Here, we can add redirection URIs required for the API connection. (For testing purpose, we should add https://localhost.authorize)
- There is no fixed pattern for the URL, We can go with the structure as per our convenience.
Step 5 Start Development for API Integration.
- In the above 4 steps, we have completed the prerequisite steps required to start development related to API Integration.
- Same as Prerequisite, we have to do some authentication steps to login into Dropbox for Upload, Download etc. operations.
DropBoxBase Class
- This class contains all the operations related to Dropbox.
- using Dropbox.Api;
- using Dropbox.Api.Files;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net.Http;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
-
- namespace DropBoxIntegration
- {
- class DropBoxBase
- {
- #region Variables
- private DropboxClient DBClient;
- private ListFolderArg DBFolders;
- private string oauth2State;
- private const string RedirectUri = "https://localhost/authorize"; // Same as we have configured Under [Application] -> settings -> redirect URIs.
- #endregion
-
- #region Constructor
- public DropBoxBase(string ApiKey, string ApiSecret, string ApplicationName = "TestApp")
- {
- try
- {
- AppKey = ApiKey;
- AppSecret = ApiSecret;
- AppName = ApplicationName;
- }
- catch (Exception)
- {
-
- throw;
- }
- }
- #endregion
- #region Properties
- public string AppName
- {
- get; private set;
- }
- public string AuthenticationURL
- {
- get; private set;
- }
- public string AppKey
- {
- get; private set;
- }
-
- public string AppSecret
- {
- get; private set;
- }
-
- public string AccessTocken
- {
- get; private set;
- }
- public string Uid
- {
- get; private set;
- }
- #endregion
-
- #region UserDefined Methods
-
-
-
-
-
- public string GeneratedAuthenticationURL()
- {
- try
- {
- this.oauth2State = Guid.NewGuid().ToString("N");
- Uri authorizeUri = DropboxOAuth2Helper.GetAuthorizeUri(OAuthResponseType.Token, AppKey, RedirectUri, state: oauth2State);
- AuthenticationURL = authorizeUri.AbsoluteUri.ToString();
- return authorizeUri.AbsoluteUri.ToString();
- }
- catch (Exception)
- {
- throw;
- }
- }
-
-
-
-
-
- public string GenerateAccessToken()
- {
- try
- {
- string _strAccessToken = string.Empty;
-
- if (CanAuthenticate())
- {
- if (string.IsNullOrEmpty(AuthenticationURL))
- {
- throw new Exception("AuthenticationURL is not generated !");
-
- }
- Login login = new Login(AppKey, AuthenticationURL, this.oauth2State);
- login.Owner = Application.Current.MainWindow;
- login.ShowDialog();
- if (login.Result)
- {
- _strAccessToken = login.AccessToken;
- AccessTocken = login.AccessToken;
- Uid = login.Uid;
- DropboxClientConfig CC = new DropboxClientConfig(AppName, 1);
- HttpClient HTC = new HttpClient();
- HTC.Timeout = TimeSpan.FromMinutes(10);
- CC.HttpClient = HTC;
- DBClient = new DropboxClient(AccessTocken, CC);
- }
- else
- {
- DBClient = null;
- AccessTocken = string.Empty;
- Uid = string.Empty;
- }
- }
-
- return _strAccessToken;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
-
-
-
-
-
-
- public bool CreateFolder(string path)
- {
- try
- {
- if (AccessTocken == null)
- {
- throw new Exception("AccessToken not generated !");
- }
- if (AuthenticationURL == null)
- {
- throw new Exception("AuthenticationURI not generated !");
- }
-
- var folderArg = new CreateFolderArg(path);
- var folder = DBClient.Files.CreateFolderAsync(folderArg);
- var result = folder.Result;
- return true;
- }
- catch (Exception ex)
- {
- return false;
- }
-
- }
-
-
-
-
-
-
- public bool FolderExists(string path)
- {
- try
- {
- if (AccessTocken == null)
- {
- throw new Exception("AccessToken not generated !");
- }
- if (AuthenticationURL == null)
- {
- throw new Exception("AuthenticationURI not generated !");
- }
-
- var folders = DBClient.Files.ListFolderAsync(path);
- var result = folders.Result;
- return true;
- }
- catch (Exception ex)
- {
- return false;
- }
- }
-
-
-
-
-
-
- public bool Delete(string path)
- {
- try
- {
- if (AccessTocken == null)
- {
- throw new Exception("AccessToken not generated !");
- }
- if (AuthenticationURL == null)
- {
- throw new Exception("AuthenticationURI not generated !");
- }
-
- var folders = DBClient.Files.DeleteAsync(path);
- var result = folders.Result;
- return true;
- }
- catch (Exception ex)
- {
- return false;
- }
- }
-
-
-
-
-
-
-
- public bool Upload(string UploadfolderPath, string UploadfileName, string SourceFilePath)
- {
- try
- {
- using (var stream = new MemoryStream(File.ReadAllBytes(SourceFilePath)))
- {
- var response = DBClient.Files.UploadAsync(UploadfolderPath + "/" + UploadfileName, WriteMode.Overwrite.Instance, body: stream);
- var rest = response.Result;
- }
-
- return true;
- }
- catch (Exception ex)
- {
- return false;
- }
-
- }
-
-
-
-
-
-
-
-
-
- public bool Download(string DropboxFolderPath, string DropboxFileName, string DownloadFolderPath, string DownloadFileName)
- {
- try
- {
- var response = DBClient.Files.DownloadAsync(DropboxFolderPath + "/" + DropboxFileName);
- var result = response.Result.GetContentAsStreamAsync();
-
- return true;
- }
- catch (Exception ex)
- {
- return false;
- }
-
- }
- #endregion
- #region Validation Methods
-
-
-
-
-
- public bool CanAuthenticate()
- {
- try
- {
- if (AppKey == null)
- {
- throw new ArgumentNullException("AppKey");
- }
- if (AppSecret == null)
- {
- throw new ArgumentNullException("AppSecret");
- }
- return true;
- }
- catch (Exception)
- {
- throw;
- }
-
- }
- #endregion
- }
- }
Login Window
- Form to redirect the user for login into Dropbox for authentication.
XAML
- <Window x:Class="DropBoxIntegration.Login"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:local="clr-namespace:DropBoxIntegration"
- mc:Ignorable="d" WindowStartupLocation="CenterOwner" WindowStyle="ToolWindow"
- Title="Login" BorderThickness="2" BorderBrush="#FF333738" Loaded="Window_Loaded">
- <Grid>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="457*"/>
- <ColumnDefinition Width="30"/>
- </Grid.ColumnDefinitions>
- <Grid.RowDefinitions>
- <RowDefinition Height="30"/>
- <RowDefinition Height="131*"/>
- </Grid.RowDefinitions>
- <Border HorizontalAlignment="Stretch"
- VerticalAlignment="Stretch" Margin="2" Grid.Row="1" Grid.ColumnSpan="2" SnapsToDevicePixels="True">
- <WebBrowser x:Name="Browser" Navigating="Browser_Navigating"/>
- </Border>
-
- </Grid>
- </Window>
.CS Class
MainWindow
- UI Interface to work with Dropbox.
Xaml
- <Window x:Class="DropBoxIntegration.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:local="clr-namespace:DropBoxIntegration"
- mc:Ignorable="d"
- Title="MainWindow" Height="321" Width="1024" MinHeight="300" MinWidth="600">
- <Grid>
- <Grid.RowDefinitions>
- <RowDefinition Height="35"/>
- <RowDefinition Height="70"/>
- <RowDefinition Height="113*"/>
- <RowDefinition Height="20"/>
- </Grid.RowDefinitions>
- <Label Content="Work with your DropBox account" FontSize="18" Foreground="Black" FontWeight="SemiBold" Margin="20,0,5,0"></Label>
- <GroupBox x:Name="gbAuthentication" HorizontalAlignment="Stretch" Margin="2" VerticalAlignment="Stretch" Grid.Row="1" Background="White">
- <GroupBox.Header>
- <Label Content="DropBox Authentication" FontWeight="SemiBold"></Label>
- </GroupBox.Header>
- <Grid>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="100"/>
- <ColumnDefinition Width="100*"/>
- <ColumnDefinition Width="100"/>
- </Grid.ColumnDefinitions>
- <Label Content="App Key: " Grid.Column="0" HorizontalContentAlignment="Right" Margin="2"/>
- <TextBox x:Name="txtApiKey" Grid.Column="1" Margin="2" MaxLength="100" Text=""></TextBox>
- <Button Content="Authenticate" x:Name="btnApiKey" Grid.Column="2" Margin="2" Click="btnApiKey_Click"></Button>
- </Grid>
-
- </GroupBox>
- <GroupBox x:Name="gbDropBox" HorizontalAlignment="Stretch" Margin="2" Grid.Row="2" VerticalAlignment="Stretch" Background="White" IsEnabled="false">
- <GroupBox.Header>
- <Label Content="DropBox Operations" FontWeight="SemiBold"></Label>
- </GroupBox.Header>
- <Grid>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="05"/>
- <ColumnDefinition Width="220*"/>
- <ColumnDefinition Width="220*"/>
- <ColumnDefinition Width="220*"/>
- <ColumnDefinition Width="220*"/>
- <ColumnDefinition Width="05"/>
- </Grid.ColumnDefinitions>
- <Grid.RowDefinitions>
- <RowDefinition Height="05"/>
- <RowDefinition Height="150*"/>
- <RowDefinition Height="05"/>
- </Grid.RowDefinitions>
- <Button x:Name="btnCreateFolder" Content="Create Folder" HorizontalAlignment="Stretch" Margin="5" VerticalAlignment="Stretch" Grid.Row="1" Grid.Column="1" FontSize="14" Click="btnCreateFolder_Click"/>
- <Button x:Name="btlUpload" Content="Upload File" HorizontalAlignment="Stretch" Margin="5" VerticalAlignment="Stretch" Grid.Row="1" Grid.Column="2" FontSize="14" Click="btlUpload_Click"/>
- <Button x:Name="btnDownload" Content="Download File" HorizontalAlignment="Stretch" Margin="5" VerticalAlignment="Stretch" Grid.Row="1" Grid.Column="3" FontSize="14" Click="btnDownload_Click"/>
- <Button x:Name="btnDelete" Content="Delete File/Directory" HorizontalAlignment="Stretch" Margin="5" VerticalAlignment="Stretch" Grid.Row="1" Grid.Column="4" FontSize="14" Click="btnDelete_Click"/>
- </Grid>
-
- </GroupBox>
-
- </Grid>
- </Window>
.CS File
- using Dropbox.Api;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
-
- namespace DropBoxIntegration
- {
-
-
-
- public partial class MainWindow : Window
- {
- #region Variables
- private string strAppKey = "[Yor Application App Key]";
- private string strAccessToken = string.Empty;
- private string strAuthenticationURL = string.Empty;
- private DropBoxBase DBB;
- #endregion
-
- #region Constructor
- public MainWindow()
- {
- InitializeComponent();
- }
- #endregion
-
- #region Private Methods
- public void Authenticate()
- {
- try
- {
- if (string.IsNullOrEmpty(strAppKey))
- {
- MessageBox.Show("Please enter valid App Key !");
- return;
- }
- if (DBB == null)
- {
- DBB = new DropBoxBase(strAppKey, "TestApp");
-
- strAuthenticationURL = DBB.GeneratedAuthenticationURL();
- strAccessToken = DBB.GenerateAccessToken();
- gbDropBox.IsEnabled = true;
- }
- else gbDropBox.IsEnabled = false;
- }
- catch (Exception)
- {
- throw;
- }
- }
- #endregion
-
- private void btnApiKey_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- strAppKey = txtApiKey.Text.Trim();
- Authenticate();
- }
- catch (Exception)
- {
- throw;
- }
-
- }
-
- private void btnCreateFolder_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- if (DBB != null)
- {
- if (strAccessToken != null && strAuthenticationURL != null)
- {
- if (DBB.FolderExists("/Dropbox/DotNetApi") == false)
- {
- DBB.CreateFolder("/Dropbox/DotNetApi");
- }
- }
- }
- }
- catch (Exception ex)
- {
- ex = ex.InnerException ?? ex;
- }
- }
-
- private void btlUpload_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- if (DBB != null)
- {
- if (strAccessToken != null && strAuthenticationURL != null)
- {
- DBB.Upload("/Dropbox/DotNetApi", "Sample-test.jpg", @"D:\Capture4-test.PNG");
- }
- }
- }
- catch (Exception)
- {
-
- throw;
- }
- }
-
- private void btnDownload_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- if (DBB != null)
- {
- if (strAccessToken != null && strAuthenticationURL != null)
- {
- DBB.Download("/Dropbox/DotNetApi", "Sample-test.jpg", @"D:\", "capture4_dwnld.png");
- }
- }
- }
- catch (Exception)
- {
-
- throw;
- }
- }
-
- private void btnDelete_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- if (DBB != null)
- {
- if (strAccessToken != null && strAuthenticationURL != null)
- {
- DBB.Delete("/Dropbox/DotNetApi");
- }
- }
- }
- catch (Exception)
- {
- throw;
- }
- }
- }
- }
That’s it. We are done with the integration. Enjoy Coding!