UWP - 如何在 C# 中将文件与 OneDrive API 同步
UWP - How to Sync files with OneDrive API in C#
我正在创建一个 UWP 应用程序,我想使用 Onedrive API 以便用户可以在他们的 Onedrive 帐户中保存他们的文件的副本,但我不明白,到目前为止我有只成功登录我想要的是:
- 上传文件
- 下载文件
- 如果其中任何一个被修改则同步
- 创建文件夹
- 删除文件
这段代码实现了登录,但我不能超越这个,上传文件或下载它们
private async void btn_Login_Click(object sender, RoutedEventArgs e)
{
if (this.oneDriveClient == null)
{
try
{
// Setting up the client here, passing in our Client Id, Return Url,
// Scopes that we want permission to, and building a Web Broker to
// go do our authentication.
this.oneDriveClient = await OneDriveClient.GetAuthenticatedMicrosoftAccountClient(
clientId,
returnUrl,
scopes,
webAuthenticationUi: new WebAuthenticationBrokerWebAuthenticationUi());
// Show in text box that we are connected.
txtBox_Response.Text = "We are now connected";
// We are either just autheticated and connected or we already connected,
// either way we need the drive button now.
btn_GetDriveId.Visibility = Visibility.Visible;
}
catch (OneDriveException exception)
{
// Eating the authentication cancelled exceptions and resetting our client.
if (!exception.IsMatch(OneDriveErrorCode.AuthenticationCancelled.ToString()))
{
if (exception.IsMatch(OneDriveErrorCode.AuthenticationFailure.ToString()))
{
txtBox_Response.Text = "Authentication failed/cancelled, disposing of the client...";
((OneDriveClient)this.oneDriveClient).Dispose();
this.oneDriveClient = null;
}
else
{
// Or we failed due to someother reason, let get that exception printed out.
txtBox_Response.Text = exception.Error.ToString();
}
}
else
{
((OneDriveClient)this.oneDriveClient).Dispose();
this.oneDriveClient = null;
}
}
}
}
我在 github 中创建了一个示例存储库:Onedrive Sync Files Sample
我已经尝试过使用Dropbox、Gdrive,但是它在UWP 上的实现似乎要复杂得多,所以我选择了OneDrive。任何答案都会非常有帮助提前致谢
How to Sync files with OneDrive API in C#
为了使用 OneDrive,我们建议您使用 OneDrive Service 实现 OneDrive 功能,它是 Windows 社区工具包 的一部分。
入门
要使用 OneDrive API,您需要有一个访问令牌,用于验证您的应用程序以获得用户的一组特定权限。在本节中,您将学习如何:
注册您的应用程序以获取客户端 ID 和客户端密码。
使用令牌流或代码流将您的用户登录到具有指定范围的 OneDrive。
注销用户(可选)。
这是official code sample,你可以参考。
我正在创建一个 UWP 应用程序,我想使用 Onedrive API 以便用户可以在他们的 Onedrive 帐户中保存他们的文件的副本,但我不明白,到目前为止我有只成功登录我想要的是:
- 上传文件
- 下载文件
- 如果其中任何一个被修改则同步
- 创建文件夹
- 删除文件
这段代码实现了登录,但我不能超越这个,上传文件或下载它们
private async void btn_Login_Click(object sender, RoutedEventArgs e)
{
if (this.oneDriveClient == null)
{
try
{
// Setting up the client here, passing in our Client Id, Return Url,
// Scopes that we want permission to, and building a Web Broker to
// go do our authentication.
this.oneDriveClient = await OneDriveClient.GetAuthenticatedMicrosoftAccountClient(
clientId,
returnUrl,
scopes,
webAuthenticationUi: new WebAuthenticationBrokerWebAuthenticationUi());
// Show in text box that we are connected.
txtBox_Response.Text = "We are now connected";
// We are either just autheticated and connected or we already connected,
// either way we need the drive button now.
btn_GetDriveId.Visibility = Visibility.Visible;
}
catch (OneDriveException exception)
{
// Eating the authentication cancelled exceptions and resetting our client.
if (!exception.IsMatch(OneDriveErrorCode.AuthenticationCancelled.ToString()))
{
if (exception.IsMatch(OneDriveErrorCode.AuthenticationFailure.ToString()))
{
txtBox_Response.Text = "Authentication failed/cancelled, disposing of the client...";
((OneDriveClient)this.oneDriveClient).Dispose();
this.oneDriveClient = null;
}
else
{
// Or we failed due to someother reason, let get that exception printed out.
txtBox_Response.Text = exception.Error.ToString();
}
}
else
{
((OneDriveClient)this.oneDriveClient).Dispose();
this.oneDriveClient = null;
}
}
}
}
我在 github 中创建了一个示例存储库:Onedrive Sync Files Sample
我已经尝试过使用Dropbox、Gdrive,但是它在UWP 上的实现似乎要复杂得多,所以我选择了OneDrive。任何答案都会非常有帮助提前致谢
How to Sync files with OneDrive API in C#
为了使用 OneDrive,我们建议您使用 OneDrive Service 实现 OneDrive 功能,它是 Windows 社区工具包 的一部分。
入门
要使用 OneDrive API,您需要有一个访问令牌,用于验证您的应用程序以获得用户的一组特定权限。在本节中,您将学习如何:
注册您的应用程序以获取客户端 ID 和客户端密码。
使用令牌流或代码流将您的用户登录到具有指定范围的 OneDrive。
注销用户(可选)。
这是official code sample,你可以参考。