Home > Net >  Problems with Google Cloud Platform authentication
Problems with Google Cloud Platform authentication

Time:03-18

we are experiencing problems with API authentication of our project in asp-net core 3.1. Specifically we have integrated the text-to-speech service provided by Google. Locally everything works correctly, but this does not happen when the web-app is online.

try
        {

            var path = "C://GoogleVoice//food-safety-trainer-47a9337eda0f.json";

            var credential = GoogleCredential.FromFile(path);
            var storage = StorageClient.Create(credential);

            TextToSpeechClient client = TextToSpeechClient.Create();
            var test = client.GrpcClient;

            // The input can be provided as text or SSML.
            SynthesisInput input = new SynthesisInput
            {
                Text = text
            };

            VoiceSelectionParams voiceSelection = new VoiceSelectionParams();
            voiceSelection.LanguageCode = "it-IT";
            voiceSelection.Name = "it-IT-Wavenet-A";
            voiceSelection.SsmlGender = SsmlVoiceGender.Female;
            
            // The audio configuration determines the output format and speaking rate.
            AudioConfig audioConfig = new AudioConfig
            {
                AudioEncoding = AudioEncoding.Mp3
            };
            SynthesizeSpeechResponse response = client.SynthesizeSpeech(input, voiceSelection, audioConfig);

            var result = _mp3Helper.SaveFile(response);
            if (result.Item1 == "Success")
                return Json(new { Result = true, Value = result.Item2 });
            else
                return Json(new { Result = false, Error = result.ToString() });
        }
        catch(Exception ex)
        {
            return Json(new { Result = false, Error = ex.Message.ToString() });
        }

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.

CodePudding user response:

Assuming you want to use the same service account for both Speech and Storage, you need to specify the credentials for the text-to-speech client. Options:

  • Set the GOOGLE_APPLICATION_DEFAULT_CREDENTIALS environment variable to refer to the JSON file. Ideally, do that as part of deployment configuration rather than in your code, but you can set the environment variable in your code if you want to. At that point, you can remove any explicit loading/setting of the credential for the Storage client.
  • Specify the CredentialPath in TextToSpeechClientBuilder:
    var client = new TextToSpeechClientBuilder { CredentialPath = path }.Build();
    
    This will load a separate credential.
  • Specify the credential's token access method via the TokenAccessMethod property in TextToSpeechClientBuilder:
    var client = new TextToSpeechClientBuilder
         {
             TokenAccessMethod = credential.GetAccessTokenForRequestAsync
         }.Build();
    
  • Related