123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- using Google.Apis.Auth.OAuth2;
- using Google.Apis.Download;
- using Google.Apis.Drive.v3;
- using Google.Apis.Services;
- using Google.Apis.Util.Store;
- using PDF_Office.Model.CloudDrive;
- using Prism.Commands;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace PDF_Office.ViewModels.HomePanel.CloudDrive
- {
-
- public class GoogleDriveViewModel
- {
- public static string[] Scopes = { DriveService.Scope.Drive };
- private static readonly string GoogleDriveAppName = "PDF Office";
- public static string FilesPathTemp { get; private set; }
- public static string CredentialsPath { get; private set; }
- public List<GoogleDriveUserItem> GoogleDriveUsers = new List<GoogleDriveUserItem>();
- public GoogleDriveViewModel()
- {
- GetFilesPathTemp();
- GetCredentialsPath();
- }
- #region 请求身份验证
- public async Task<bool> 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<DriveService, UserCredential> 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<List<Tuple<DriveService, UserCredential>>> GetHistoryService()
- {
- DirectoryInfo TheFolder = new DirectoryInfo(GoogleDriveViewModel.FilesPathTemp);
- List<Tuple<DriveService, UserCredential>> DriveServices = new List<Tuple<DriveService, UserCredential>>();
- foreach (var directorieItem in TheFolder.GetDirectories())
- {
- var driveServiceItem = await GetServiceAsync(directorieItem.Name);
- if (driveServiceItem != null)
- {
- DriveServices.Add(driveServiceItem);
- }
- }
- return DriveServices;
- }
-
-
-
-
- [Obsolete]
- public async Task<Tuple<DriveService, UserCredential>> GetServiceAsync(string userInfoFile = "")
- {
- Tuple<DriveService, UserCredential> tuple = null;
- await Task.Run(() =>
- {
- tuple = GetService(userInfoFile);
- });
- return tuple;
- }
-
-
-
-
-
- [Obsolete]
- private Tuple<DriveService, UserCredential> GetService(string FilePath = "")
- {
- Tuple<DriveService, UserCredential> tuple = null;
- UserCredential credential;
- if (FilePath == "")
- {
- var time = DateTime.Now.ToString("yyyyMMddHHmmss");
- FilePath = System.IO.Path.Combine(GoogleDriveViewModel.FilesPathTemp, time + ".json");
- }
- using (var stream = new FileStream(GoogleDriveViewModel.CredentialsPath, FileMode.Open, FileAccess.Read))
- {
- credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
- GoogleClientSecrets.Load(stream).Secrets,
- Scopes,
- "user",
- CancellationToken.None,
- new FileDataStore(FilePath, true)).Result;
- }
-
- DriveService service = new DriveService(new BaseClientService.Initializer()
- {
- HttpClientInitializer = credential,
- ApplicationName = GoogleDriveAppName
- });
- return tuple = new Tuple<DriveService, UserCredential>(service, credential);
- }
- #endregion
- #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
- }
- }
|