Ejemplo n.º 1
0
        /// <summary>Creates a user credential from JSON data.</summary>
        private static UserCredential CreateUserCredentialFromParameters(JsonCredentialParameters credentialParameters)
        {
            if (credentialParameters.Type != JsonCredentialParameters.AuthorizedUserCredentialType ||
                string.IsNullOrEmpty(credentialParameters.ClientId) ||
                string.IsNullOrEmpty(credentialParameters.ClientSecret))
            {
                throw new InvalidOperationException("JSON data does not represent a valid user credential.");
            }

            var token = new TokenResponse
            {
                RefreshToken = credentialParameters.RefreshToken
            };

            var initializer = new GoogleAuthorizationCodeFlow.Initializer
            {
                ClientSecrets = new ClientSecrets
                {
                    ClientId     = credentialParameters.ClientId,
                    ClientSecret = credentialParameters.ClientSecret
                },
                ProjectId = credentialParameters.ProjectId
            };
            var flow = new GoogleAuthorizationCodeFlow(initializer);

            return(new UserCredential(flow, "ApplicationDefaultCredentials", token));
        }
Ejemplo n.º 2
0
        /// <summary>Creates a <see cref="ServiceAccountCredential"/> from JSON data.</summary>
        private static ServiceAccountCredential CreateServiceAccountCredentialFromParameters(
            JsonCredentialParameters credentialParameters)
        {
            if (credentialParameters.Type != JsonCredentialParameters.ServiceAccountCredentialType ||
                string.IsNullOrEmpty(credentialParameters.ClientEmail) ||
                string.IsNullOrEmpty(credentialParameters.PrivateKey))
            {
                throw new InvalidOperationException("JSON data does not represent a valid service account credential.");
            }
            var initializer = new ServiceAccountCredential.Initializer(credentialParameters.ClientEmail);

            initializer.ProjectId = credentialParameters.ProjectId;
            return(new ServiceAccountCredential(initializer.FromPrivateKey(credentialParameters.PrivateKey)));
        }
Ejemplo n.º 3
0
        /// <summary>Creates a default credential from JSON data.</summary>
        private static GoogleCredential CreateDefaultCredentialFromParameters(JsonCredentialParameters credentialParameters)
        {
            switch (credentialParameters.Type)
            {
            case JsonCredentialParameters.AuthorizedUserCredentialType:
                return(new GoogleCredential(CreateUserCredentialFromParameters(credentialParameters)));

            case JsonCredentialParameters.ServiceAccountCredentialType:
                return(GoogleCredential.FromServiceAccountCredential(
                           CreateServiceAccountCredentialFromParameters(credentialParameters)));

            default:
                throw new InvalidOperationException(
                          String.Format("Error creating credential from JSON. Unrecognized credential type {0}.",
                                        credentialParameters.Type));
            }
        }