Microsoft Graph SDK C#:将FileSavePicker与Onedrive一起使用

时间:2016-12-19 20:16:48

标签: uwp onedrive microsoft-graph filesavepicker

是否可以使用FileSavePicker对象使用Microsoft Graph SDK for C#在Onedrive中保存文件。

原因是我需要用户选择数据备份文件的存储位置。

使用以下源代码,我可以下载或上传文件,但不会提示用户选择所需的路径。

Authentication.cs

internal class Authentication
{

    internal static GraphServiceClient GetAuthenticatedClient()
    {
        if (Settings.graphClient == null)
        {
            try
            {
                Settings.graphClient = new GraphServiceClient("https://graph.microsoft.com/v1.0", new DelegateAuthenticationProvider(
                            async (requestMessage) =>
                            {
                                var token = await GetTokenForUserAsync();

                                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token);
                            })
                        );

                return Settings.graphClient;
            }
            catch (Exception)
            { }
        }
        return Settings.graphClient;
    }


    internal static async Task<string> GetTokenForUserAsync()
    {
        AuthenticationResult authResult;

        try
        {
            authResult = await Settings.IdentityClientApp.AcquireTokenSilentAsync(Settings.Scopes);

            Settings.TokenForUser = authResult.Token;
        }
        catch (Exception)
        {
            if (Settings.TokenForUser == null || Settings.Expiration <= DateTimeOffset.UtcNow.AddMinutes(5))
            {
                authResult = await Settings.IdentityClientApp.AcquireTokenAsync(Settings.Scopes);

                Settings.TokenForUser = authResult.Token;

                Settings.Expiration = authResult.ExpiresOn;
            }
        }

        return Settings.TokenForUser;
    }


    internal static void SignOut()
    {
        foreach (var user in Settings.IdentityClientApp.Users)

            user.SignOut();

        Settings.graphClient = null;

        Settings.TokenForUser = null;
    }

}

Settings.cs

internal class Settings
{
    public static string clientId = "xxxxxx-xxxx-xxx-xxx-xxxxxxxxx";

    public static string[] Scopes = {
                                        "https://graph.microsoft.com/User.Read",
                                        "https://graph.microsoft.com/Mail.Send",
                                        "https://graph.microsoft.com/Files.ReadWrite.All",
                                        "https://graph.microsoft.com/Files.ReadWrite" };

    public static PublicClientApplication IdentityClientApp = new PublicClientApplication(clientId);

    public static string TokenForUser = null;

    public static DateTimeOffset Expiration;

    public static GraphServiceClient graphClient = null;
}

下载文件

        public static async Task<bool> Download(string file, string to)
        {
            try
            {
                var download = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(file), Uri.EscapeUriString(System.IO.Path.GetFileName(file)));

                using (Stream stream = await Authentication.GetAuthenticatedClient().Me.Drive.Root.ItemWithPath(download).Content.Request().GetAsync())
                {
                    try
                    {
                        using (FileStream writer = new FileStream(to, FileMode.Create))
                        {

                        }

                        using (StreamReader reader = new StreamReader(stream))
                        {
                            await PathIO.WriteTextAsync(to, reader.ReadToEnd());
                        }

                        return true;
                    }
                    catch (Exception)
                    {
                        return false;
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

1 个答案:

答案 0 :(得分:1)

你的问题对我来说并不完全清楚;我了解您希望将FilePicker与OneDrive应用程序一起使用,然后强制用户在OneDrive中选择目标?

这是不可能的。我认为最好的方法是构建自己的UI,让用户选择一个文件夹并使用上面的代码将备份文件保存到onedrive