Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. internal static class Perm_AppData
  2. {
  3. private static string[] scopes = { DriveService.Scope.DriveFile };
  4. private static DriveService service = Autorization();
  5. internal static DriveService Service { get { return service; } }
  6.  
  7. private static DriveService Autorization()
  8. {
  9. UserCredential credential;
  10.  
  11. using (var stream = GetCliSecStream())
  12. {
  13. credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
  14. GoogleClientSecrets.Load(stream).Secrets,
  15. scopes,
  16. "user",
  17. CancellationToken.None,
  18. new FileDataStore(auth, true)).Result;
  19. }
  20.  
  21. return new DriveService(new BaseClientService.Initializer()
  22. {
  23. HttpClientInitializer = credential,
  24. ApplicationName = applicationName
  25. });
  26. }
  27.  
  28. private static Stream GetCliSecStream()
  29. {
  30. var stream = new MemoryStream();
  31. var writer = new StreamWriter(stream);
  32. writer.Write(uSec);
  33. writer.Flush();
  34. stream.Position = 0;
  35. return stream;
  36. }
  37. }
  38.  
  39. internal static MemoryStream DownloadFile(string fileId = null, string fileName = null)
  40. {
  41. using (new Watcher($"Download {fileName} {fileId}"))
  42. {
  43. GetRequest request = null;
  44. if (fileId != null)
  45. {
  46. request = service.Files.Get(fileId);
  47. }
  48. else
  49. {
  50. if (fileName != null)
  51. {
  52. request = service.Files.Get(ViewDrive(SearchParameter.name, new string[] { fileName }).First().Id);
  53. }
  54. else
  55. {
  56. throw new Exception("You need at least one not null parameter to download");
  57. }
  58. }
  59.  
  60. request.Fields = "id, name";
  61. var fileMemoryStream = new MemoryStream();
  62. request.Download(fileMemoryStream);
  63. return fileMemoryStream;
  64. }
  65. }
  66.  
  67. WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement