Search code examples
.net-6.0google-speech-to-text-apigoogle-cloud-api-gateway

How to use Google Service Account json file in .NET 6


I'm trying to create a .NET 6 web app that connects to Google Cloud API's, and host my app on Azure

I've created a service account with "Owner" permission, downloaded the json file, and placed the json file in the root of my directory.

How can I instruct my app to use this when connecting to Google Cloud API?

I see that I can set a windows environment variable, but I want to do it in my solution since it will be uploaded to Azure cloud


Solution

  • How can I instruct my app to use this when connecting to Google Cloud API?

    Refer to this code for example: Authenticating with a service account key file

    BigQuery Client Example:

    var credentials = GoogleCredential.FromFile(jsonPath);
    var client = BigQueryClient.Create(projectId, credentials);
    

    Note from GCP Authentication documentation:

    Creating a service account key is a security risk that should be avoided if possible. If you must create a service account key, make sure you keep it secure. Anyone with access to the key can gain all of the access that's granted to the associated service account.

    The recommended solution is Workload Identity Federation

    Check this page for configuring Workload Identity Federation to access GCP resources from Azure