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

福斯杰夫

我正在创建一个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。任何答案都会非常有帮助,谢谢

Nico Zhu - MSFT

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

对于使用OneDrive,我们建议您使用Windows社区工具包中的OneDrive服务实现OneDrive功能

入门

要使用OneDrive API,您需要具有一个访问令牌,该令牌可以对您的应用进行身份验证,以获取用户的特定权限集。在本节中,您将学习如何:

  1. 注册您的应用程序以获取客户端ID和客户端密钥。

  2. 使用令牌流或代码流将用户登录到具有指定范围的OneDrive。

  3. 注销用户(可选)。

这是您可以参考的官方代码示例

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章