1

I'm trying to set up a very simple test application for connecting to my Firebase Cloud Firestore instance.

It's a C# .NET Core console application using .NET Core 3.0 on Windows 7

I'm just trying to connecting to my Firestore instance using my project name.

My GOOGLE_APPLICATION_CREDENTIALS environment variable is set to C:\test\creds.json which I created from using the "Create Key" feature on the GCP Console page for my default service account.

When I run the code below, I get the following error:

using Google.Cloud.Firestore;
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            FirestoreDb db = FirestoreDb.Create("my-proj-id");
        }
    }
}

Exception:

The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.'

When I run ProcessMonitor to detect file system activity, my application never even tries to touch the creds.json file, which makes me believe the C# Cloud Firestore API isn't even finding my creds file. I've set the GOOGLE_APPLICATION_CREDENTIALS env variable at both the user and system level.

Is there a problem with how I've configured things?

Mike Marshall
  • 7,788
  • 4
  • 39
  • 63
  • 1
    how are you getting `GOOGLE_APPLICATION_CREDENTIALS`? can you log this `string value = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS);` – LinkedListT Jan 27 '20 at 19:20
  • I'm not reading the environment variable. According to this link (https://firebase.google.com/docs/firestore/quickstart), in your developer environment you merely set GOOGLE_APPLICATION_CREDENTIALS to the key file location to authenticate any requests. – Mike Marshall Jan 27 '20 at 19:32
  • @LinkedListT FYI, in powershell "[System.Environment]::GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS")" yields the right path to my creds file – Mike Marshall Jan 27 '20 at 19:36
  • are you actually running as that user? (We don't have any context of how you're running this.) – LinkedListT Jan 27 '20 at 19:50
  • I'm confused. This is .NET Core 3.0 Console app on Windows. I'm logged in to Windows as a domain user, not sure that matters. Google developer documentation says to create a security key (JSON file - c:\test\creds.json) from the Google Cloud Console. I created a key for the "App Engine default service account", which is listed as service account in the GCP console. I set GOOGLE_APPLICATION_CREDENTIALS to "c:\test\creds.json" per the Google documentation. When I try to access my instance from C#, it basically says the creds are not found, but I see no disk activity to c:\temp\creds.json – Mike Marshall Jan 27 '20 at 19:57
  • @LinkedListT My apologies, now I see what you were getting at. I tried to log the value of the env variable and it was null. I restarted Visual Studio and all of a sudden it started to work :/ – Mike Marshall Jan 27 '20 at 20:53

2 Answers2

1

Well this was maddening. I tried to log the GOOGLE_APPLICATION_CREDENTIALS environment variable value in my app and it was null. I restarted Visual Studio and it started to work. My theories are that either VS passed the current environment to the debugee when it launches the debugger process or that it may use one of those Visual Studio debugger host proxy processes for debugging and it was restarted after I restarted the IDE

Mike Marshall
  • 7,788
  • 4
  • 39
  • 63
0

Sometimes the environment that Visual Studio is running can be different to the scope of the PATH ENV variables in Windows. I think it is related to the Admin permission. It would be interesting to execute the task and the GOOGLE_APPLICATION_CREDENTIALS assignment opening Visual Studio as Administrator.

Seems that this behavior is expected. C# .net MVC, set path to Google Application Credentials JSON file

Mario
  • 406
  • 2
  • 6
  • I believe it is just as simple as my app inherits environment variables from Visual Studio. If you start Visual Studio before you set the environment variable, then you need to restart Visual Studio to have the changes picked up. – Mike Marshall Jan 28 '20 at 22:20