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

时间:2017-07-26 22:04:01

标签: google-cloud-platform google-speech-api

我遵循Google的Speech API快速入门文档,为帐户启用结算和API。此帐户已授权服务帐户代表其创建Compute实例。在子帐户上创建实例后,托管二进制文件以使用Speech 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 ="发生异常)在   元数据凭证插件。")

     

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

     

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

     

at Grpc.Core.Internal.AsyncCall`2.UnaryCall(TRequest msg)

     

在   Grpc.Core.Calls.BlockingUnaryCall [TRequest,τ响应(CallInvocationDetails`2   电话,请求请求)

     

在   Grpc.Core.DefaultCallInvoker.BlockingUnaryCall [TRequest,τ响应(Method`2   方法,字符串主机,CallOptions选项,TRequest请求)

     

在   Grpc.Core.Internal.InterceptingCallInvoker.BlockingUnaryCall [TRequest,τ响应(Method`2   方法,字符串主机,CallOptions选项,TRequest请求)

     

在   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)

     

at Google.Api.Gax.Grpc.ApiCall`2.Sync(TRequest request,   CallSettings perCallCallSettings)

     

在   Google.Cloud.Speech.V1.SpeechClientImpl.Recognize(RecognizeRequest   请求,CallSettings callSettings)

     

at Google.Cloud.Speech.V1.SpeechClient.Recognize(RecognitionConfig   config,RecognitionAudio audio,CallSettings callSettings)

     

在Rc2Solver.frmMain.RecognizeWordsGoogleSpeechApi()中   C:\ Users \用户尔达\谷歌   Drive \ VSProjects \ Rc2Solver \ Rc2Solver \ frmMain.cs:1770行

     

---------------------------确定

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

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

                );

我在网上找不到有关为服务帐户参与者专门授权或验证Speech API的信息或代码。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

事实证明,问题在于需要使用指定的ServiceAccount参数创建Cloud Compute实例。否则,Cloud实例不是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");
  }
 });