简体   繁体   English

Google Cloud Platform 身份验证问题

[英]Problems with Google Cloud Platform authentication

we are experiencing problems with API authentication of our project in asp.net core 3.1.我们在 asp.net 核心 3.1 中对我们的项目进行 API 身份验证时遇到问题。 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.如果在 Google Compute Engine 中运行,它们就可用。 Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials.否则,环境变量 GOOGLE_APPLICATION_CREDENTIALS 必须定义为指向定义凭据的文件。 See https://developers.google.com/accounts/docs/application-default-credentials for more information.有关详细信息,请参阅https://developers.google.com/accounts/docs/application-default-credentials

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.设置GOOGLE_APPLICATION_DEFAULT_CREDENTIALS环境变量以引用 JSON 文件。 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 :TextToSpeechClientBuilder中指定CredentialPath
     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 :通过TextToSpeechClientBuilder中的TokenAccessMethod属性指定凭证的令牌访问方法:
     var client = new TextToSpeechClientBuilder { TokenAccessMethod = credential.GetAccessTokenForRequestAsync }.Build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM