2016-12-19 4 views
0

FileSavePicker 개체를 사용하여 C# 용 Microsoft Graph SDK를 사용하여 Onedrive에서 파일을 저장할 수 있습니까?Microsoft Graph SDK C# : Onedrive와 FileSavePicker 사용

이유는 사용자가 데이터 백업 파일을 저장할 위치를 선택해야하기 때문입니다.

다음 소스 코드를 사용하여 파일을 다운로드하거나 업로드 할 수 있지만 원하는 경로를 선택하라는 메시지가 표시되지 않습니다.

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

; OneDrive 앱에서 FilePicker를 사용하고 OneDrive에서 대상을 선택하도록 강요합니다.

이것은 불가능합니다. 가장 좋은 방법은 사용자가 폴더를 선택하고 위에있는 코드를 사용하여 백업 파일을 onedrive에 저장하도록 사용자 고유의 UI를 구축하는 것입니다.