Beispiel #1
0
        public StorageService CreateStorageClient()
        {
            var tokenServerUrl = "";
            var credentials    = new Google.Apis.Auth.OAuth2.ComputeCredential();
            var initializer    = new Google.Apis.Auth.OAuth2.ServiceCredential.Initializer(tokenServerUrl);


            //if (credentials.IsCreateScopedRequired)
            //{
            //    credentials = credentials.CreateScoped(new[] { StorageService.Scope.DevstorageFullControl });
            //}

            var serviceInitializer = new BaseClientService.Initializer()
            {
                ApplicationName       = "Storage Sample",
                HttpClientInitializer = credentials
            };

            return(new StorageService(serviceInitializer));
        }
        /// <summary>Creates a new default credential.</summary>
        private async Task <GoogleCredential> CreateDefaultCredentialAsync()
        {
            // 1. First try the environment variable.
            string credentialPath = GetEnvironmentVariable(CredentialEnvironmentVariable);

            if (!String.IsNullOrWhiteSpace(credentialPath))
            {
                try
                {
                    return(CreateDefaultCredentialFromFile(credentialPath));
                }
                catch (Exception e)
                {
                    // Catching generic exception type because any corrupted file could manifest in different ways
                    // including but not limited to the System, System.IO or from the Newtonsoft.Json namespace.
                    throw new InvalidOperationException(
                              String.Format("Error reading credential file from location {0}: {1}"
                                            + "\nPlease check the value of the Environment Variable {2}",
                                            credentialPath,
                                            e.Message,
                                            CredentialEnvironmentVariable), e);
                }
            }

            // 2. Then try the well known file.
            credentialPath = GetWellKnownCredentialFilePath();
            if (!String.IsNullOrWhiteSpace(credentialPath))
            {
                try
                {
                    return(CreateDefaultCredentialFromFile(credentialPath));
                }
                catch (FileNotFoundException)
                {
                    // File is not present, eat the exception and move on to the next check.
                    Logger.Debug("Well-known credential file {0} not found.", credentialPath);
                }
                catch (DirectoryNotFoundException)
                {
                    // Directory not present, eat the exception and move on to the next check.
                    Logger.Debug("Well-known credential file {0} not found.", credentialPath);
                }
                catch (Exception e)
                {
                    throw new InvalidOperationException(
                              String.Format("Error reading credential file from location {0}: {1}"
                                            + "\nPlease rerun 'gcloud auth login' to regenerate credentials file.",
                                            credentialPath,
                                            e.Message), e);
                }
            }

            // 3. Then try the compute engine.
            Logger.Debug("Checking whether the application is running on ComputeEngine.");
            if (await ComputeCredential.IsRunningOnComputeEngine().ConfigureAwait(false))
            {
                Logger.Debug("ComputeEngine check passed. Using ComputeEngine Credentials.");
                return(new GoogleCredential(new ComputeCredential()));
            }

            // If everything we tried has failed, throw an exception.
            throw new InvalidOperationException(
                      String.Format("The Application Default Credentials are not available. They are available if running"
                                    + " in Google Compute Engine. Otherwise, the environment variable {0} must be defined"
                                    + " pointing to a file defining the credentials. See {1} for more information.",
                                    CredentialEnvironmentVariable,
                                    HelpPermalink));
        }
 public void IsRunningOnComputeEngine_ResultIsCached()
 {
     // Two subsequent invocations should return the same task.
     Assert.AreSame(ComputeCredential.IsRunningOnComputeEngine(),
                    ComputeCredential.IsRunningOnComputeEngine());
 }
 public void IsRunningOnComputeEngine()
 {
     // It should be safe to assume that this test is not running on GCE.
     Assert.IsFalse(ComputeCredential.IsRunningOnComputeEngine().Result);
 }
 public ComputeGoogleCredential(ComputeCredential credential)
 {
     this.credential = credential;
 }
 /// <summary>Creates a <c>GoogleCredential</c> wrapping a <see cref="ComputeCredential"/>.</summary>
 internal static GoogleCredential FromCredential(ComputeCredential credential)
 {
     return new ComputeGoogleCredential(credential);
 }
 internal Initializer(ComputeCredential other)
     : base(other) => OidcTokenUrl = other.OidcTokenUrl;
 /// <summary>
 /// Create a <see cref="GoogleCredential"/> from a <see cref="ComputeCredential"/>.
 /// In general, do not use this method. Call <see cref="GetApplicationDefault"/> or
 /// <see cref="GetApplicationDefaultAsync"/>, which will provide the most suitable
 /// credentials for the current platform.
 /// </summary>
 /// <param name="computeCredential">Optional. The compute credential to use in the returned <see cref="GoogleCredential"/>.
 /// If <c>null</c>, then a new <see cref="ComputeCredential"/> will be instantiated, using the default
 /// <see cref="ComputeCredential.Initializer"/>.</param>
 /// <returns>A <see cref="GoogleCredential"/> with an underlying <see cref="ComputeCredential"/>.</returns>
 public static GoogleCredential FromComputeCredential(ComputeCredential computeCredential = null) =>
 new GoogleCredential(computeCredential ?? new ComputeCredential());