Esempio n. 1
0
        /// <summary>
        /// Creates Google Drive service object with correct credentials.
        /// </summary>
        /// <param name="refreshToken">Refresh Token to use.</param>
        /// <returns>Google Drive service object.</returns>
        private async Task <DriveService> CreateService(string refreshToken)
        {
            var initializer =
                new AuthorizationCodeFlow.Initializer(
                    "https://accounts.google.com/o/oauth2/auth",
                    "https://accounts.google.com/o/oauth2/token")
            {
                ClientSecrets =
                    new ClientSecrets
                {
                    ClientId     = Constants.GoogleDriveSyncClientId,
                    ClientSecret = Constants.GoogleDriveSyncClientSecret
                }
            };

            // Hardcoded Google Drive Sync secret.
            string[] scopes = { DriveService.Scope.DriveReadonly };
            initializer.Scopes = scopes;

            using (var codeFlow = new AuthorizationCodeFlow(initializer))
            {
                try
                {
                    var token = await GoogleRequestHelper.Execute(
                        ct => codeFlow.RefreshTokenAsync("user", refreshToken, ct),
                        this.CancellationToken);

                    var credential = new UserCredential(codeFlow, "user", token);

                    var service =
                        new DriveService(
                            new BaseClientService.Initializer
                    {
                        HttpClientInitializer = credential,
                        ApplicationName       = Utils.Constants.ApplicationName
                    });

                    return(service);
                }
                catch (Exception e)
                {
                    if (this.CancellationToken.IsCancellationRequested)
                    {
                        this.Log.LogMessage("Operation was cancelled.");
                        throw;
                    }

                    this.StatusAccumulator.FailureOccurred();
                    this.Log.LogError("Authorization error.", e);
                    throw;
                }
            }
        }
Esempio n. 2
0
        public async Task connectionmaker()
        {
            var init = new GoogleAuthorizationCodeFlow.Initializer
            {
                ClientSecrets = new ClientSecrets
                {
                    ClientId     = this.dbconf.ClientID,
                    ClientSecret = this.dbconf.Clientsecret
                },
                Scopes = new string[] { "https://www.googleapis.com/auth/drive" }
            };
            var flow = new AuthorizationCodeFlow(init);

            Token = await flow.RefreshTokenAsync("user", dbconf.RefreshToken, CancellationToken.None);

            credential = GoogleCredential.FromAccessToken(Token.AccessToken);
            service    = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = this.dbconf.ApplicationName,
            });
        }