/// <summary>
        /// Obtains credentials asynchronously. Override this method in a concrete builder type if more
        /// credential mechanisms are supported.
        /// </summary>
        protected async virtual Task <IConfigurableHttpClientInitializer> GetHttpClientInitializerAsync(CancellationToken cancellationToken)
        {
            if (Credential != null)
            {
                return(Credential);
            }
            GoogleCredential unscoped =
                CredentialsPath != null ? await GoogleCredential.FromFileAsync(CredentialsPath, cancellationToken).ConfigureAwait(false) :
                JsonCredentials != null?GoogleCredential.FromJson(JsonCredentials) :
                    null; // Use default credentials (maybe - see below)

            // While we accept any credentials that are specified even if there's an API key,
            // we don't try to load default credentials.
            if (ApiKey != null && unscoped is null)
            {
                return(QuotaProject is null ? null :
                       new ExtraHeadersInitializer(
                           new AccessTokenWithHeaders.Builder {
                    QuotaProject = QuotaProject
                }.Build(null)));
            }
            GoogleCredential scoped = await GetScopedCredentialProvider().GetCredentialsAsync(unscoped, cancellationToken).ConfigureAwait(false);

            return(QuotaProject is null ? scoped : scoped.CreateWithQuotaProject(QuotaProject));
        }
示例#2
0
        /// <summary>
        /// Obtains credentials synchronously. Override this method in a concrete builder type if more
        /// credential mechanisms are supported.
        /// </summary>
        protected virtual IConfigurableHttpClientInitializer GetHttpClientInitializer()
        {
            if (Credential != null)
            {
                return(Credential);
            }
            GoogleCredential unscoped =
                GoogleCredential != null ? GoogleCredential :
                CredentialsPath != null?GoogleCredential.FromFile(CredentialsPath) :
                    JsonCredentials != null?GoogleCredential.FromJson(JsonCredentials) :
                        null; // Use default credentials (maybe - see below)

            // While we accept any credentials that are specified even if there's an API key,
            // we don't try to load default credentials.
            if (ApiKey != null && unscoped is null)
            {
                return(QuotaProject is null ? null :
                       new ExtraHeadersInitializer(
                           new AccessTokenWithHeaders.Builder {
                    QuotaProject = QuotaProject
                }.Build(null)));
            }
            GoogleCredential scoped = GetScopedCredentialProvider().GetCredentials(unscoped);

            return(QuotaProject is null ? scoped : scoped.CreateWithQuotaProject(QuotaProject));
        }
        public async Task AccessToken_WithQuotaProjectHeader(GoogleCredential credential)
        {
            credential = credential.CreateWithQuotaProject("my-billing-project");
            var accessToken = await((ITokenAccessWithHeaders)credential.UnderlyingCredential).GetAccessTokenWithHeadersForRequestAsync("https://api.googleapis.com/");

            AssertContainsQuotaProject(
                accessToken.Headers.Select(pair => new KeyValuePair <string, IEnumerable <string> >(pair.Key, pair.Value)),
                "my-billing-project");
        }
        public async Task Intercepts_WithQuotaProject(GoogleCredential credential)
        {
            credential = credential.CreateWithQuotaProject("my-billing-project");

            HttpRequestHeaders headers;

            using (var interceptor = new FakeHandler())
                using (var httpClient = new ConfigurableHttpClient(new ConfigurableMessageHandler(interceptor)))
                {
                    ((IConfigurableHttpClientInitializer)credential).Initialize(httpClient);
                    await httpClient.GetAsync("http://will.be.ignored");

                    headers = interceptor.RequestHeaders;
                }

            AssertContainsQuotaProject(headers, "my-billing-project");
        }
        public void CreateWithQuotaProject_NotSame(GoogleCredential credential)
        {
            var newCredetial = credential.CreateWithQuotaProject("my-quota-project");

            Assert.NotSame(credential, newCredetial);
        }
        public void CreateWithQuotaProject(GoogleCredential credential)
        {
            var newCredetial = credential.CreateWithQuotaProject("my-quota-project");

            Assert.Equal("my-quota-project", newCredetial.QuotaProject);
        }