public async Task <ActionResult> Disconnect(CancellationToken taskCancellationToken)
        {
            try
            {
                if (DateTime.UtcNow.Subtract(CurrentUser.GoogleDriveAccount.ConnectingDate).TotalSeconds + 300 >
                    CurrentUser.GoogleDriveAccount.ExpiresInSeconds)
                {
                    CurrentUser.GoogleDriveAccount = GoogleDriveApi.RefreshUser(CurrentUser.StringId, CurrentUser.GoogleDriveAccount);
                    UnitOfWork.Commit();
                }

                var token      = CurrentUser.GoogleDriveAccount;
                var credential = new UserCredential(AppFlow.Flow, CurrentUser.StringId, new TokenResponse
                {
                    AccessToken      = token.AccessToken,
                    ExpiresInSeconds = token.ExpiresInSeconds,
                    Issued           = token.Issued,
                    RefreshToken     = token.RefreshToken,
                    Scope            = token.Scope,
                    TokenType        = token.TokenType
                });

                await credential.RevokeTokenAsync(taskCancellationToken);
            }
            catch { }
            UnitOfWork.GoogleDriveRepository.Delete(CurrentUser.Id);
            UnitOfWork.Commit();
            return(RedirectToAction <UserController>(c => c.Index()));
        }
Ejemplo n.º 2
0
        private bool GoogleDrive(User user, byte[] data)
        {
            try
            {
                OnAccountStart(new BackupEventArgs(AccountEnum.GoogleDrive.ToString()));
                if (DateTime.UtcNow.Subtract(user.GoogleDriveAccount.ConnectingDate).TotalSeconds + 300 >
                    user.GoogleDriveAccount.ExpiresInSeconds)
                {
                    user.GoogleDriveAccount = GoogleDriveApi.RefreshUser(user.StringId, user.GoogleDriveAccount);
                    _unitOfWork.Commit();
                }

                DriveService service  = CreateDriveService(user);
                string       folderId = GetDriveFolderId(service);
                UploadDriveFile(service, data, folderId);

                OnAccountComplete(new BackupEventArgs(AccountEnum.GoogleDrive.ToString()));
                return(true);
            }
            catch (Exception ex)
            {
                OnShowError(new BackupEventArgs(AccountEnum.GoogleDrive.ToString()));
                logger.Error("Problems with Google Drive", ex);
                _unitOfWork.GoogleDriveRepository.Delete(user.Id);
                _unitOfWork.Commit();
                return(false);
            }
        }