/// <summary>The core logic for asynchronously authorizing the specified user.</summary>
        /// <param name="initializer">The authorization code initializer.</param>
        /// <param name="scopes">
        /// The scopes which indicate the Google API access your application is requesting.
        /// </param>
        /// <param name="user">The user to authenticate.</param>
        /// <param name="taskCancellationToken">Cancellation token to cancel an operation.</param>
        /// <returns>User credential.</returns>
        private static async Task<UserCredential> AuthorizeAsyncCore(AuthorizationCodeFlow.Initializer initializer,
            IEnumerable<string> scopes, string user, CancellationToken taskCancellationToken)
        {
            initializer.Scopes = scopes;
            initializer.DataStore = new StorageDataStore();

            var installedApp = new AuthorizationCodeWPInstalledApp(initializer);
            return await installedApp.AuthorizeAsync(user, taskCancellationToken).ConfigureAwait(false);
        }
 /// <summary>
 /// Constructs a new initializer from the given <see cref="AuthorizationCodeFlow"/>
 /// </summary>
 internal Initializer(AuthorizationCodeFlow flow)
 {
     AccessMethod           = flow.AccessMethod;
     TokenServerUrl         = flow.TokenServerUrl;
     AuthorizationServerUrl = flow.AuthorizationServerUrl;
     ClientSecrets          = flow.ClientSecrets;
     DataStore         = flow.DataStore;
     Scopes            = flow.Scopes;
     HttpClientFactory = flow.HttpClientFactory;
     DefaultExponentialBackOffPolicy = flow.DefaultExponentialBackOffPolicy;
     Clock = flow.Clock;
 }
        private void EnsureService()
        {
            if (!_setting.UseGoogleDrive)
            {
                return;
            }

            if (_driveService != null)
            {
                return;
            }

            if (string.IsNullOrEmpty(_setting.GoogleClientId) || string.IsNullOrEmpty(_setting.GoogleClientSecret) ||
                 string.IsNullOrEmpty(_setting.GoogleRefreshToken))
            {
                throw new ApplicationException("Missing google drive configuration");
            }

            try
            {
                var initializer = new GoogleAuthorizationCodeFlow.Initializer
                {
                    ClientSecrets = new ClientSecrets
                    {
                        ClientId = _setting.GoogleClientId,
                        ClientSecret = _setting.GoogleClientSecret
                    }
                };
                var flow = new AuthorizationCodeFlow(initializer);
                //flow.RefreshTokenAsync("user", Configuration.GoogleRefreshToken, new CancellationTokenSource().Token);

                var tokenResponse = new TokenResponse { RefreshToken = _setting.GoogleRefreshToken };
                var userCredential = new UserCredential(flow, _setting.GoogleLocalUserId, tokenResponse);

                _driveService = new DriveService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = userCredential,
                    ApplicationName = "Jarboo.Admin"
                });
            }
            catch (Exception ex)
            {
                _driveService = null;

                throw;
            }
        }
Example #4
0
        private static UserCredential GetCredentialWithAccessToken(string refreshToken, string clientID, string clientSecret, string[] scopes)
        {
            TokenResponse token = new TokenResponse
            {
                RefreshToken = refreshToken
            };

            IAuthorizationCodeFlow flow = new AuthorizationCodeFlow(new AuthorizationCodeFlow.Initializer(Google.Apis.Auth.OAuth2.GoogleAuthConsts.AuthorizationUrl, Google.Apis.Auth.OAuth2.GoogleAuthConsts.TokenUrl)
            {
                ClientSecrets = new ClientSecrets
                {
                    ClientId = clientID,
                    ClientSecret = clientSecret
                },
                Scopes = scopes
            });

            UserCredential credential = new UserCredential(flow, "me", token);
            try
            {
                bool success = credential.RefreshTokenAsync(CancellationToken.None).Result;
            }
            catch
            {
                throw;
            }
            return credential;
        }
 /// <summary>Constructs a new authorization code installed application for Windows Store.</summary>
 /// <param name="authorizationCodeFlowInitializer">A authorization code flow initializer</param>
 public AuthorizationCodeWinRTInstalledApp(AuthorizationCodeFlow.Initializer authorizationCodeFlowInitializer)
 {
     this.authorizationCodeFlowInitializer = authorizationCodeFlowInitializer;
 }
Example #6
0
 /// <summary>Constructs a new Google authorization code flow.</summary>
 public AAGoogleAuthorizationCodeFlow(AuthorizationCodeFlow.Initializer initializer)
     : base(initializer)
 {
 }
 /// <summary>
 /// Constructs a new authorization code for Windows Store application targeting an installed application flow.
 /// </summary>
 /// <param name="authorizationCodeFlowInitializer">An authorization code flow initializer.</param>
 public AuthorizationCodeWindowsInstalledApp(AuthorizationCodeFlow.Initializer authorizationCodeFlowInitializer)
 {
     innerInstallApp = new AuthorizationCodeInstalledApp(
         new AuthorizationCodeFlow(authorizationCodeFlowInitializer), new AuthorizationCodeBroker());
 }
 /// <summary>Constructs a new authorization code installed application for WP.</summary>
 /// <param name="authorizationCodeFlowInitializer">A authorization code flow initializer.</param>
 public AuthorizationCodeWPInstalledApp(AuthorizationCodeFlow.Initializer authorizationCodeFlowInitializer)
 {
     var flow = new AuthorizationCodeFlow(authorizationCodeFlowInitializer);
     innerInstallApp = new AuthorizationCodeInstalledApp(flow, new AuthorizationCodeBroker());
 }
        /// <summary>The core logic for asynchronously authorizing the specified user.</summary>
        /// <param name="initializer">The authorization code initializer.</param>
        /// <param name="scopes">
        /// The scopes which indicate the Google API access your application is requesting.
        /// </param>
        /// <param name="user">The user to authorize.</param>
        /// <param name="taskCancellationToken">Cancellation token to cancel an operation.</param>
        /// <param name="dataStore">The data store, if not specified a file data store will be used.</param>
        /// <returns>User credential.</returns>
        private static async Task<UserCredential> AuthorizeAsyncCore(AuthorizationCodeFlow.Initializer initializer,
            IEnumerable<string> scopes, string user, CancellationToken taskCancellationToken,
            IDataStore dataStore = null)
        {
            initializer.Scopes = scopes;
            initializer.DataStore = dataStore ?? new FileDataStore(Folder);
            var flow = new GoogleAuthorizationCodeFlow(initializer);

            // Create authorization code installed app instance and authorize the user.
            return await new AuthorizationCodeInstalledApp(flow, new LocalServerCodeReceiver()).AuthorizeAsync
                (user, taskCancellationToken).ConfigureAwait(false);
        }