Search code examples
c#authenticationgoogle-cloud-platformgoogle-client

can I use another custom environment variable name instead of GOOGLE_APPLICATION_CREDENTIALS?


in order to enable multiple credentials to my application - I want to use custom environment variable for each.

how can I set it explicitly?

in GCP docs I see only this default option.

Edit:

the libraries I'm using are Google.Cloud.Storage.V1 and Google.Apis.Dataflow.v1b3

I succeeded by using this

var credentials = GoogleCredential.FromFile(@"c:\certs\sa.json");
var storageClient = StorageClient.Create(credentials);

Solution

  • If you use the default credential creation, only this environment variable is possible

    (example in python)

    import google.auth
    
    credentials, project_id = google.auth.default()
    

    Now, think at the value that you put in the GOOGLE_APPLICATION_CREDENTIALS env var. It's simply a path to a service account key file. So, you can do that, which is equivalent to the previous command

    import google.auth
    
    credentials = service_account.Credentials.from_service_account_file(os.getenv('GOOGLE_APPLICATION_CREDENTIALS'))
    

    And thus, you can use the env var name that you want and create an explicit credential by passing the path to the correct file!

    Example in C#:

    var credentials = GoogleCredential.FromFile(@"c:\certs\trigger-sa.json");
    var storageClient = StorageClient.Create(credentials);