谷歌云平台身份验证问题



我们在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_APPLICATION_CREDENTIALS。看见https://developers.google.com/accounts/docs/application-default-credentials了解更多信息。

假设您想对Speech和Storage使用相同的服务帐户,则需要为文本到语音客户端指定凭据。选项:

  • 设置GOOGLE_APPLICATION_DEFAULT_CREDENTIALS环境变量以引用JSON文件。理想情况下,将其作为部署配置的一部分而不是在代码中执行,但如果愿意,可以在代码中设置环境变量。此时,您可以删除存储客户端凭据的任何显式加载/设置
  • TextToSpeechClientBuilder中指定CredentialPath
    var client = new TextToSpeechClientBuilder { CredentialPath = path }.Build();
    
    这将加载一个单独的凭据
  • 通过TextToSpeechClientBuilder中的TokenAccessMethod属性指定凭据的令牌访问方法:
    var client = new TextToSpeechClientBuilder
    {
    TokenAccessMethod = credential.GetAccessTokenForRequestAsync
    }.Build();
    

最新更新