using Google.Apis.Auth.OAuth2; using Google.Apis.Drive.v3; using Google.Apis.Services; using Google.Apis.Util.Store; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace PDF_Office.ViewModels.HomePanel.CloudDrive { //多用户UX交互 public class GoogleDriveManager { public static string[] Scopes = { DriveService.Scope.Drive }; //Google Drive应用名称 private static readonly string GoogleDriveAppName = "PDF Office"; //请求应用进行身份验证的信息 public static string CredentialsPath { get; private set; } //存放已通过身份验证的用户信息,以便下次不用登录便可使用云文档 public static string FilesPathTemp { get; private set; } //已登录的用户们 public List GoogleDriveUsers = new List(); public GoogleDriveManager() { GetFilesPathTemp(); GetCredentialsPath(); } #region 云文档的用户帐户缓存路径和身份验证文件 /// /// 获取或创建缓存登录帐户信息 /// public void GetFilesPathTemp() { string str_1 = System.AppDomain.CurrentDomain.BaseDirectory; String FolderPath = str_1 + "GoogleDriveUsers"; if (Directory.Exists(FolderPath) == false) Directory.CreateDirectory(FolderPath); FilesPathTemp = FolderPath; } /// /// 获取本地身份验证文件 /// public void GetCredentialsPath() { string str_1 = System.AppDomain.CurrentDomain.BaseDirectory; String filePath = str_1 + @"\credentials.json"; CredentialsPath = filePath; } #endregion #region 请求身份验证 /// /// 获取登录过的用户 /// public async Task GetHistoryUsers() { var tuples = await GetHistoryService(); GoogleDriveUsers.Clear(); foreach (var tuple in tuples) { if (tuple != null && tuple.Item1 != null && tuple.Item2 != null) { AddGoogleDriveUser(tuple); } } if (GoogleDriveUsers.Count > 0) { return true; } return false; } public async void AddGoogleDriveUser(Tuple tuple) { GoogleDriveUserItem user = new GoogleDriveUserItem(); user.Service = tuple.Item1; user.User.CurrentCredential = tuple.Item2; user.User.UserAccount = await user.GetUserAcountAsync(); GoogleDriveUsers.Add(user); } /// /// 获取登录过的账号 /// /// 历史账号 private async Task>> GetHistoryService() { DirectoryInfo TheFolder = new DirectoryInfo(GoogleDriveManager.FilesPathTemp); List> DriveServices = new List>(); foreach (var directorieItem in TheFolder.GetDirectories()) { var driveServiceItem = await GetServiceAsync(directorieItem.Name); if (driveServiceItem != null) { DriveServices.Add(driveServiceItem); } } return DriveServices; } /// /// 异步获取Google服务的包信息,避免UI线程卡死 /// /// [Obsolete] public async Task> GetServiceAsync(string userInfoFile = "") { Tuple tuple = null; await Task.Run(() => { tuple = GetService(userInfoFile); }); return tuple; } /// /// 获取Google服务的包信息(包含访问令牌,App Key密钥等) /// /// 登录过的用户文件;若为空,则为新用户登录 /// [Obsolete] private Tuple GetService(string FilePath = "") { Tuple tuple = null; UserCredential credential; if (FilePath == "") { var time = DateTime.Now.ToString("yyyyMMddHHmmss"); FilePath = System.IO.Path.Combine(GoogleDriveManager.FilesPathTemp, time + ".json"); } using (var stream = new FileStream(GoogleDriveManager.CredentialsPath, FileMode.Open, FileAccess.Read)) { credential = GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(stream).Secrets, Scopes, "user", CancellationToken.None, new FileDataStore(FilePath, true)).Result; } //create Drive API service. DriveService service = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = GoogleDriveAppName }); return tuple = new Tuple(service, credential); } #endregion } }