我遵循了Google的语音API的快速启动文档,以便为帐户启用计费和API。此帐户已授权服务帐户代表它创建计算实例。在子帐户上创建一个实例,托管一个二进制文件以使用语音API之后,我无法在C#语音示例中成功地使用谷歌提供的示例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()行上失败,出现以下错误:
(在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task任务)
(在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task任务)
(在Grpc.Core.Internal.AsyncCall`2.UnaryCall(TRequest msg)
在Grpc.Core.Calls.BlockingUnaryCallTRequest,TResponse
在Grpc.Core.DefaultCallInvoker.BlockingUnaryCallTRequest,TResponse
在Grpc.Core.Internal.InterceptingCallInvoker.BlockingUnaryCallTRequest,TResponse
应Google.Cloud.Speech.V1.Speech.SpeechClient.Recognize(RecognizeRequest请求,CallOptions选项)
在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请求,CallSettings callSettings)
在Google.Api.Gax.Grpc.ApiCall`2.Sync(TRequest request,CallSettings perCallCallSettings)
应Google.Cloud.Speech.V1.SpeechClientImpl.Recognize(RecognizeRequest请求,CallSettings callSettings)
在Google.Cloud.Speech.V1.SpeechClient.Recognize(RecognitionConfig配置,RecognitionAudio音频,CallSettings callSettings)
在C中的Rc2Solver.frmMain.RecognizeWordsGoogleSpeechApi():\Users\jorda\Google Drive\VSProjects\Rc2Solver\Rc2Solver\frmMain.cs:line 1770
-好的
我已证实语音API已被激活。以下是服务帐户在创建Compute实例时使用的范围:
credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(me)
{
Scopes = new[] { ComputeService.Scope.Compute, ComputeService.Scope.CloudPlatform }
}.FromPrivateKey(yk)
);我在网上没有发现任何关于为服务帐户参与者专门授权或验证语音API的信息或代码。任何帮助都是非常感谢的。
发布于 2017-07-27 19:57:04
事实证明,问题是需要使用指定的ServiceAccount参数来创建Compute实例。否则,云实例不是ServiceAccount默认凭据的一部分,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");
}
});https://stackoverflow.com/questions/45338194
复制相似问题