Beispiel #1
0
        public static Connection Create(GoogleDriveUserCredential credential)
        {
            Connection connection = new Connection();

            connection.Connect(credential);

            if (!connection.IsConnected())
            {
                throw new BusinessRuleException("Cannot connect to GoogleDrive service.");
            }
            return(connection);
        }
Beispiel #2
0
        private void Connect(GoogleDriveUserCredential clientSecretsData)
        {
            if (String.IsNullOrEmpty(clientSecretsData.ClientId))
            {
                throw new ArgumentNullException("ClientID", "ClientID wasn't specified.");
            }
            if (String.IsNullOrEmpty(clientSecretsData.ClientSecret))
            {
                throw new ArgumentNullException("ClientSecret", "ClientSecret wasn't specified.");
            }
            if (String.IsNullOrEmpty(clientSecretsData.DataStore))
            {
                throw new ArgumentNullException("DataStore", "DataStore wasn't specified.");
            }
            if (!Directory.Exists(clientSecretsData.DataStore))
            {
                throw new DirectoryNotFoundException($"{clientSecretsData.DataStore} directory is either invalid or wasn't found.");
            }

            UserCredential cr = GoogleWebAuthorizationBroker.AuthorizeAsync(
                new ClientSecrets()
            {
                ClientId = clientSecretsData.ClientId, ClientSecret = clientSecretsData.ClientSecret
            },
                Connection.Scopes,
                Connection.UserIdentifier,
                CancellationToken.None,
                new FileDataStore(clientSecretsData.DataStore, true)).Result;

            credential = cr;

            Service = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = Connection.ApplicationName,
            });
        }