Google Cloud Platform 身份验证问题

发布于 2025-01-14 18:17:46 字数 1956 浏览 2 评论 0原文

我们在 asp-net core 3.1 中的项目的 API 身份验证方面遇到问题。具体来说,我们集成了谷歌提供的文本转语音服务。在本地,一切正常,但当网络应用程序在线时,这种情况不会发生。

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() });
        }

应用程序默认凭据不可用。如果在 Google Compute Engine 中运行,则它们可用。否则,必须定义环境变量 GOOGLE_APPLICATION_CREDENTIALS 指向定义凭据的文件。请参阅 https://developers.google.com/accounts/docs/application-default -凭据了解更多信息。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伪心 2025-01-21 18:17:46

假设您希望对语音和存储使用相同的服务帐户,则需要指定文本转语音客户端的凭据。选项:

  • 设置 GOOGLE_APPLICATION_DEFAULT_CREDENTIALS 环境变量以引用 JSON 文件。理想情况下,将其作为部署配置的一部分而不是在代码中执行,但如果您愿意,您可以在代码中设置环境变量。此时,您可以删除存储客户端凭据的任何显式加载/设置。
  • TextToSpeechClientBuilder 中指定 CredentialPath
    var client = new TextToSpeechClientBuilder { CredentialPath = 路径 }.Build();
    

    这将加载一个单独的凭据。

  • 通过 TextToSpeechClientBuilder 中的 TokenAccessMethod 属性指定凭据的令牌访问方法:
    var client = new TextToSpeechClientBuilder
         {
             TokenAccessMethod = credential.GetAccessTokenForRequestAsync
         }。建造();
    

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();
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文