Google 云平台身份验证问题
Problems with Google Cloud Platform authentication
我们在 asp-net core 3.1 中的项目 API 身份验证遇到问题。具体来说,我们集成了 Google 提供的文字转语音服务。在本地一切正常,但当网络应用在线时不会发生这种情况。
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.
假设您要为语音和存储使用相同的服务帐户,您需要为 text-to-speech 客户端指定凭据。选项:
- 设置
GOOGLE_APPLICATION_DEFAULT_CREDENTIALS
环境变量来引用JSON文件。理想情况下,将其作为部署配置的一部分而不是在代码中执行,但如果需要,您 可以 在代码中设置环境变量。届时,您可以删除存储客户端凭据的任何显式 loading/setting。
- 在
TextToSpeechClientBuilder
中指定CredentialPath
:
var client = new TextToSpeechClientBuilder { CredentialPath = path }.Build();
这将加载一个单独的凭据。
- 通过
TextToSpeechClientBuilder
中的 TokenAccessMethod
属性 指定凭据的令牌访问方法:
var client = new TextToSpeechClientBuilder
{
TokenAccessMethod = credential.GetAccessTokenForRequestAsync
}.Build();
我们在 asp-net core 3.1 中的项目 API 身份验证遇到问题。具体来说,我们集成了 Google 提供的文字转语音服务。在本地一切正常,但当网络应用在线时不会发生这种情况。
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.
假设您要为语音和存储使用相同的服务帐户,您需要为 text-to-speech 客户端指定凭据。选项:
- 设置
GOOGLE_APPLICATION_DEFAULT_CREDENTIALS
环境变量来引用JSON文件。理想情况下,将其作为部署配置的一部分而不是在代码中执行,但如果需要,您 可以 在代码中设置环境变量。届时,您可以删除存储客户端凭据的任何显式 loading/setting。 - 在
TextToSpeechClientBuilder
中指定CredentialPath
:
这将加载一个单独的凭据。var client = new TextToSpeechClientBuilder { CredentialPath = path }.Build();
- 通过
TextToSpeechClientBuilder
中的TokenAccessMethod
属性 指定凭据的令牌访问方法:var client = new TextToSpeechClientBuilder { TokenAccessMethod = credential.GetAccessTokenForRequestAsync }.Build();