简体   繁体   English

UWP - 如何在 C# 中与 OneDrive API 同步文件

[英]UWP - How to Sync files with OneDrive API in C#

I am creating a UWP application and I want to use the Onedrive API so that users can save a copy of their files in their Onedrive account, but I don't get it, so far I have only managed to login what I want is:我正在创建一个 UWP 应用程序,我想使用 Onedrive API 以便用户可以将他们的文件副本保存在他们的 Onedrive 帐户中,但我不明白,到目前为止,我只能设法登录我想要的是:

  • upload files上传文件
  • download files下载文件
  • and synchronize if any of them is modified如果其中任何一个被修改,则同步
  • Create folders创建文件夹
  • Delete Files删除文件

This code achieves the login, but I can not move beyond this, as would proceed for upload files or download them这段代码实现了登录,但我不能超越这一点,就像继续上传文件或下载它们一样

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

I created a sample repository in github: Onedrive Sync Files Sample我在 github 中创建了一个示例存储库: Onedrive 同步文件示例

I have already tried using Dropbox, Gdrive, but its implementation for UWP seems to be much more complex, so I chose OneDrive.我已经尝试过使用 Dropbox、Gdrive,但它对 UWP 的实现似乎要复杂得多,所以我选择了 OneDrive。 any answer will be very helpful thanks in advance任何答案都会非常有帮助,在此先感谢

How to Sync files with OneDrive API in C#如何在 C# 中与 OneDrive API 同步文件

For using OneDrive, we suggest you implement OneDrive feature with OneDrive Service that is part of Windows Community Toolkit .对于使用 OneDrive,我们建议您使用OneDrive 服务实现 OneDrive 功能,该服务是Windows 社区工具包的一部分。

Getting Started入门

To use the OneDrive API, you need to have an access token that authenticates your app to a particular set of permissions for a user.要使用 OneDrive API,您需要拥有一个访问令牌,该令牌可针对用户的一组特定权限对您的应用进行身份验证。 In this section, you'll learn how to:在本节中,您将学习如何:

  1. Register your application to get a client ID and a client secret.注册您的应用程序以获取客户端 ID 和客户端密码。

  2. Sign your user in to OneDrive with the specified scopes using the token flow or code flow.使用令牌流或代码流将用户登录到具有指定范围的 OneDrive。

  3. Sign the user out (optional).将用户注销(可选)。

And this is official code sample that you could refer.这是您可以参考的官方代码示例

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

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