Exemplo n.º 1
0
        public async static Task <GoogleDriveProvider> Instance(GoogleDriveAccount acc)
        {
            GoogleDriveProvider provider = new GoogleDriveProvider();
            await provider.Authenticate(acc);

            return(provider);
        }
        public static GoogleDriveAccount RefreshUser(string userId, GoogleDriveAccount googleDriveAccount)
        {
            var credential = new UserCredential(AppFlow.Flow, userId, new TokenResponse
            {
                AccessToken      = googleDriveAccount.AccessToken,
                ExpiresInSeconds = googleDriveAccount.ExpiresInSeconds,
                Issued           = googleDriveAccount.Issued,
                RefreshToken     = googleDriveAccount.RefreshToken,
                Scope            = googleDriveAccount.Scope,
                TokenType        = googleDriveAccount.TokenType
            });
            Task <bool> refresh = credential.RefreshTokenAsync(new CancellationToken());

            Task.WaitAll(refresh);
            return(new GoogleDriveAccount
            {
                ConnectingDate = DateTime.UtcNow,
                Enabled = googleDriveAccount.Enabled,
                AccessToken = credential.Token.AccessToken,
                RefreshToken = credential.Token.RefreshToken,
                Issued = credential.Token.Issued,
                Scope = credential.Token.Scope,
                TokenType = credential.Token.TokenType,
                ExpiresInSeconds = credential.Token.ExpiresInSeconds,
                AccountName = googleDriveAccount.AccountName
            });
        }
Exemplo n.º 3
0
        public async Task Authenticate(GoogleDriveAccount acc)
        {
            object threadlock = new Object();

            GoogleAuthorizationCodeFlow.Initializer init = new GoogleAuthorizationCodeFlow.Initializer();
            init.ClientSecrets              = new ClientSecrets();
            init.ClientSecrets.ClientId     = AppInfo.GoogleClientId;
            init.ClientSecrets.ClientSecret = AppInfo.GoogleClientSecret;
            GoogleAuthorizationCodeFlow f = new GoogleAuthorizationCodeFlow(init);

            Google.Apis.Auth.OAuth2.Responses.TokenResponse t = null;

            UserCredential credential = new UserCredential(f, acc.UserId, acc.UserToken);
            await credential.RefreshTokenAsync(CancellationToken.None);

            driveService = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = "SilentBackup",
            });
        }