GoogleDriveManager.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using Google.Apis.Auth.OAuth2;
  2. using Google.Apis.Drive.v3;
  3. using Google.Apis.Services;
  4. using Google.Apis.Util.Store;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace PDF_Office.ViewModels.HomePanel.CloudDrive
  13. {
  14. //多用户UX交互
  15. public class GoogleDriveManager
  16. {
  17. public static string[] Scopes = { DriveService.Scope.Drive };
  18. //Google Drive应用名称
  19. private static readonly string GoogleDriveAppName = "PDF Office";
  20. //请求应用进行身份验证的信息
  21. public static string CredentialsPath { get; private set; }
  22. //存放已通过身份验证的用户信息,以便下次不用登录便可使用云文档
  23. public static string FilesPathTemp { get; private set; }
  24. //已登录的用户们
  25. public List<GoogleDriveUserItem> GoogleDriveUsers = new List<GoogleDriveUserItem>();
  26. public GoogleDriveManager()
  27. {
  28. GetFilesPathTemp();
  29. GetCredentialsPath();
  30. }
  31. #region 云文档的用户帐户缓存路径和身份验证文件
  32. /// <summary>
  33. /// 获取或创建缓存登录帐户信息
  34. /// </summary>
  35. public void GetFilesPathTemp()
  36. {
  37. string str_1 = System.AppDomain.CurrentDomain.BaseDirectory;
  38. String FolderPath = str_1 + "GoogleDriveUsers";
  39. if (Directory.Exists(FolderPath) == false)
  40. Directory.CreateDirectory(FolderPath);
  41. FilesPathTemp = FolderPath;
  42. }
  43. /// <summary>
  44. /// 获取本地身份验证文件
  45. /// </summary>
  46. public void GetCredentialsPath()
  47. {
  48. string str_1 = System.AppDomain.CurrentDomain.BaseDirectory;
  49. String filePath = str_1 + @"\credentials.json";
  50. CredentialsPath = filePath;
  51. }
  52. #endregion
  53. #region 请求身份验证
  54. /// <summary>
  55. /// 获取登录过的用户
  56. /// </summary>
  57. public async Task<bool> GetHistoryUsers()
  58. {
  59. var tuples = await GetHistoryService();
  60. GoogleDriveUsers.Clear();
  61. foreach (var tuple in tuples)
  62. {
  63. if (tuple != null && tuple.Item1 != null && tuple.Item2 != null)
  64. {
  65. AddGoogleDriveUser(tuple);
  66. }
  67. }
  68. if (GoogleDriveUsers.Count > 0)
  69. {
  70. return true;
  71. }
  72. return false;
  73. }
  74. public async void AddGoogleDriveUser(Tuple<DriveService, UserCredential> tuple)
  75. {
  76. GoogleDriveUserItem user = new GoogleDriveUserItem();
  77. user.Service = tuple.Item1;
  78. user.User.CurrentCredential = tuple.Item2;
  79. user.User.UserAccount = await user.GetUserAcountAsync();
  80. GoogleDriveUsers.Add(user);
  81. }
  82. /// <summary>
  83. /// 获取登录过的账号
  84. /// </summary>
  85. /// <returns>历史账号</returns>
  86. private async Task<List<Tuple<DriveService, UserCredential>>> GetHistoryService()
  87. {
  88. DirectoryInfo TheFolder = new DirectoryInfo(GoogleDriveManager.FilesPathTemp);
  89. List<Tuple<DriveService, UserCredential>> DriveServices = new List<Tuple<DriveService, UserCredential>>();
  90. foreach (var directorieItem in TheFolder.GetDirectories())
  91. {
  92. var driveServiceItem = await GetServiceAsync(directorieItem.Name);
  93. if (driveServiceItem != null)
  94. {
  95. DriveServices.Add(driveServiceItem);
  96. }
  97. }
  98. return DriveServices;
  99. }
  100. /// <summary>
  101. /// 异步获取Google服务的包信息,避免UI线程卡死
  102. /// </summary>
  103. /// <param name="userInfoFile"></param>
  104. [Obsolete]
  105. public async Task<Tuple<DriveService, UserCredential>> GetServiceAsync(string userInfoFile = "")
  106. {
  107. Tuple<DriveService, UserCredential> tuple = null;
  108. await Task.Run(() =>
  109. {
  110. tuple = GetService(userInfoFile);
  111. });
  112. return tuple;
  113. }
  114. /// <summary>
  115. /// 获取Google服务的包信息(包含访问令牌,App Key密钥等)
  116. /// </summary>
  117. /// <param name="FilePath">登录过的用户文件;若为空,则为新用户登录</param>
  118. /// <returns></returns>
  119. [Obsolete]
  120. private Tuple<DriveService, UserCredential> GetService(string FilePath = "")
  121. {
  122. Tuple<DriveService, UserCredential> tuple = null;
  123. UserCredential credential;
  124. if (FilePath == "")
  125. {
  126. var time = DateTime.Now.ToString("yyyyMMddHHmmss");
  127. FilePath = System.IO.Path.Combine(GoogleDriveManager.FilesPathTemp, time + ".json");
  128. }
  129. using (var stream = new FileStream(GoogleDriveManager.CredentialsPath, FileMode.Open, FileAccess.Read))
  130. {
  131. credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
  132. GoogleClientSecrets.Load(stream).Secrets,
  133. Scopes,
  134. "user",
  135. CancellationToken.None,
  136. new FileDataStore(FilePath, true)).Result;
  137. }
  138. //create Drive API service.
  139. DriveService service = new DriveService(new BaseClientService.Initializer()
  140. {
  141. HttpClientInitializer = credential,
  142. ApplicationName = GoogleDriveAppName
  143. });
  144. return tuple = new Tuple<DriveService, UserCredential>(service, credential);
  145. }
  146. #endregion
  147. }
  148. }