7

Title says it all pretty much.

I tried uploading the json file to azure storage and referenced it's url when setting the GOOGLE_APPLICATION_CREDENTIALS environment variable under app settings, but when remotely debugging the site, apparently the url/directory was not in an acceptable format. I can’t store the json file locally either because the website doesn’t have any idea about my C drive directories.

Where should I store this file so that I can set the GOOGLE_APPLICATION_CREDENTIALS environment variable for my azure site to the directory of the json file?

5 Answers 5

4

The ToChannelCredentials() approach does not seem to work anymore, so I come up with an other solution that works on Azure. I create a text file in the /bin folder of my Azure server with the credentials and then I point the environment variable to this file. Google Cloud API will use this for the default credentials.

string json = @"{
    'type': 'service_account',
    'project_id': 'xxx',
    'private_key_id': 'xx',
    'private_key': 'xxx',
     ...
}";  // this is the content of the json-credentials file from Google

// Create text file in projects bin-folder 
var binDirectory = Path.GetDirectoryName(Assembly.GetCallingAssembly().CodeBase);
string fullPath = Path.Combine(binDirectory, "credentials.json").Replace("file:\\","");

using (StreamWriter outputFile = new StreamWriter(fullPath, false)) {
    outputFile.WriteLine(json);
}

// Set environment variabel to the full file path
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", fullPath);

// Now you can call the service and it will pick up your credentials
TranslationServiceClient client = TranslationServiceClient.Create(); 
3
  • This works for me when running locally. I have a suspicion it will also work on Azure, EXCEPT that I get an error: Access to the path 'C:\\Program Files (x86)\\dotnet\\shared\\Microsoft.NETCore.App\\3.1.27\\credentials.json' is denied.
    – Luc Bloom
    Sep 5, 2022 at 14:00
  • On a Windows based plan - the writable directory is: D:\home\ I'll try to write to D:\home\credentials.json
    – Luc Bloom
    Sep 5, 2022 at 14:02
  • I now got an "internal server error" response.
    – Luc Bloom
    Sep 5, 2022 at 14:09
3

If anyone is wondering how to handle the Google's credentials smoothly in .Net applications instead of strange way of using the file on server, this is how I solved it for Translation Service. Other services must follow same principle:

  • store the content of the Google credentials json file as an environment variable in settings.json/azure configuration for your app (using ' ' instead of " " for inner text):

    "GOOGLE_APPLICATION_CREDENTIALS": "{'type': 'service_account','project_id': ...}"
    
  • create and return the client:

      var credential = GoogleCredential.FromJson(Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS"));
      var channelCredentials = credential.ToChannelCredentials();
      var channel = new Channel(TranslationServiceClient.DefaultEndpoint.ToString(), channelCredentials);
      return TranslationServiceClient.Create(channel);
    

Took a while for me to figure it our. Hope it helps.

2

I use the .json file in my local environment (because of environment variable length limit in Windows) and on Azure I use an "Application setting" to set an environment variable. This code handles both cases:

string? json;
var filename = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");
if (filename != null)
{
    json = System.IO.File.ReadAllText(filename);
}
else
{
    json = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS_STRING");
    if (json == null)
    {
        throw new Exception(
            "GOOGLE_APPLICATION_CREDENTIALS_STRING environment variable with JSON is not set");
    }
}

var credential = GoogleCredential.FromJson(json).ToChannelCredentials();
var grpcChannel = new Channel("firestore.googleapis.com", credential);
var grcpClient = new Firestore.FirestoreClient(grpcChannel);
var firestoreClient = new FirestoreClientImpl(grcpClient, FirestoreSettings.GetDefault());
return await FirestoreDb.CreateAsync(FirebaseProjectId, firestoreClient);
3
  • The code above uses the Google.Cloud.Firestore and FirebaseAdmin nugets Jun 3, 2020 at 13:16
  • This is the best answer. The usings are: using Google.Cloud.Firestore.V1; using Google.Apis.Auth.OAuth2; using Grpc.Core; using Grpc.Auth;
    – Luc Bloom
    Sep 6, 2022 at 7:09
  • 1
    Locally, you can also use Properties\launchSettings.json to set the environment variable. It worked for me, it was not too long.
    – Luc Bloom
    Sep 6, 2022 at 7:11
0

Was looking how to set the "GOOGLE_APPLICATION_CREDENTIALS" in Azure App Service. The answers here didn't help me. My solution is very simple without any code change.

  • In the configuration of the app service go to the Path Mappings
  • Add a New Azure Storage Mount. eg /mounts/config
  • Add the credentials.json file to the file share
  • In the application settings, add the GOOGLE_APPLICATION_CREDENTIALS and set the value to: /mounts/config/credentials.json

That is all.

1
  • 2
    It doesn't feel right to put the credentials file in a Storage Mount , I'd like to get it from keyVault Aug 25, 2022 at 8:50
-2

In the azure app on the azure portal go to application settings and add the credentials under application settings tab

Then you can reference them in your code as they were in your web.config file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.