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

3

我正在创建一个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。感谢您提前给予任何帮助。
1个回答

2
如何在C#中使用OneDrive API同步文件
如果要使用OneDrive,我们建议您使用Windows社区工具包的一部分OneDrive服务来实现OneDrive功能。
入门指南
要使用OneDrive API,您需要有一个访问令牌,该访问令牌对用户的特定权限集进行身份验证。在这个部分,您将学习如何:
1. 注册您的应用程序以获取客户端ID和客户端密钥。 2. 使用令牌流或代码流以指定范围登录您的用户到OneDrive。 3. 登出用户(可选)。
这是官方代码示例,您可以参考。

1
我还建议阅读这个答案,以了解在掌握API调用本身后处理同步过程的最佳方法。 - Brad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接