通过服务帐号创建的实例无法使用 Google Cloud 语音 API - 身份验证错误



我按照Google的语音API快速入门文档为帐户启用计费和API。此帐户已授权服务帐户代表其创建计算实例。在儿童帐户上创建实例并托管二进制文件以使用语音 API 后,我无法在 C# 语音示例中成功使用 Google 提供的示例 C# 代码:

try
{
var speech = SpeechClient.Create();                
var response = speech.Recognize(new RecognitionConfig()
{
Encoding = RecognitionConfig.Types.AudioEncoding.Linear16,
LanguageCode = "en"
}, RecognitionAudio.FromFile(audioFiles[0]));
foreach (var result in response.Results)
{
foreach (var alternative in result.Alternatives)
{
Debug.WriteLine(alternative.Transcript);
}
}
} catch (Exception ex)
// ...
}

请求在SpeechClient.Create()行上失败,并显示以下错误:


--------------------------- grpc.Core.RpcException: Status(StatusCode=Unauthenticated, detail="Exception 發生在 元数据凭据插件。

在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task 任务(

在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task 任务(

at Grpc.Core.Internal.AsyncCall'2.UnaryCall(TRequest msg(

在 Grpc.Core.Calls.BlockingUnaryCall[TRequest,TResponse](CallInvocationDetails'2 呼叫,请求请求(

在 Grpc.Core.DefaultCallInvoker.BlockingUnaryCall[TRequest,TResponse](Method'2 方法、字符串主机、调用选项选项、TRequest 请求(

在 Grpc.Core.Internal.InterceptingCallInvoker.BlockingUnaryCall[TRequest,TResponse](Method'2 方法、字符串主机、调用选项选项、TRequest 请求(

在 Google.Cloud.Speech.V1.Speech.SpeechClient.Recognize(RecognizeRequest 请求,呼叫选项选项(

在 Google.Api.Gax.Grpc.ApiCall.<>c__DisplayClass0_0'2.b__1(TRequest req, CallSettings cs(

在 Google.Api.Gax.Grpc.ApiCallRetryExtensions.<>c__DisplayClass1_0'2.b__0(TRequest 请求,呼叫设置调用设置(

at Google.Api.Gax.Grpc.ApiCall'2.Sync(TRequest request, 呼叫设置每个呼叫呼叫设置(

在 Google.Cloud.Speech.V1.SpeechClientImpl.Recognize(RecognizeRequest 请求,呼叫设置调用设置(

at Google.Cloud.Speech.V1.SpeechClient.Recognize(RecognitionConfig 配置、识别音频、呼叫设置呼叫设置(

at Rc2Solver.frmMain.RecognizeWordsGoogleSpeechApi(( in C:\Users\jorda\Google 驱动器\VSProjects\Rc2求解器\Rc2求解器\frmMain.cs:line 1770

---------------------------还行

我已验证语音 API 是否已激活。下面是服务帐户在创建计算实例时使用的范围:

credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(me)
{
Scopes = new[] { ComputeService.Scope.Compute, ComputeService.Scope.CloudPlatform }
}.FromPrivateKey(yk)
);

我在网上找不到有关专门为服务帐户参与者授权或验证语音 API 的信息或代码。任何帮助,不胜感激。

事实证明,问题是需要使用指定的服务帐户参数创建云计算实例。否则,云实例不是服务帐户默认凭据的一部分,该凭据由SpeechClient.Create()调用引用。以下是创建附加到服务帐户的实例的正确方法,它将使用与项目 ID 绑定的 SA:

service = new ComputeService(new BaseClientService.Initializer() {
HttpClientInitializer = credential,
ApplicationName = "YourAppName"
});
string MyProjectId = "example-project-27172";
var project = await service.Projects.Get(MyProjectId).ExecuteAsync();
ServiceAccount servAcct = new ServiceAccount() {
Email = project.DefaultServiceAccount,
Scopes = new [] {
"https://www.googleapis.com/auth/cloud-platform"
}
};

Instance instance = new Instance() {
MachineType = service.BaseUri + MyProjectId + "/zones/" + targetZone + "/machineTypes/" + "g1-small",
Name = name,
Description = name,
Disks = attachedDisks,
NetworkInterfaces = networkInterfaces,
ServiceAccounts = new [] {
servAcct
},
Metadata = md
};
batchRequest.Queue < Instance > (service.Instances.Insert(instance, MyProjectId, targetZone),
(content, error, i, message) => {
if (error != null) {
AddEventMsg("Error creating instance " + name + ": " + error.ToString());
} else {
AddEventMsg("Instance " + name + " created");
}
});

最新更新