首页 文章

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

提问于
浏览
0

我按照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 =“元数据凭证插件中出现异常 . “)在Grpc.Core的Grpc.Core.Internal.AsyncCall2.UnaryCall(TRequest msg)的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)上的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) .Calls.BlockingUnaryCall [TRequest,TResponse](CallInvocationDetails2调用,TRequest req)在Grpc.Core的Grpc.Core.DefaultCallInvoker.BlockingUnaryCall [TRequest,TResponse](方法2方法,字符串主机,CallOptions选项,TRequest请求) .Internal.InterceptingCallInvoker.BlockingUnaryCall [TRequest,TResponse](方法2方法,字符串主机,CallOptions选项,TRequest请求)在谷歌的Google.Cloud.Speech.V1.Speech.SpeechClient.Recognize(RecognizeRequest request,CallOptions options) . Google.Api.Gax.Grpc.ApiCallRe上的Api.Gax.Grpc.ApiCall . <> c__DisplayClass0_02.b__1(请求请求,CallSettings cs)在Google.Cloud.Speech.V1.SpeechClientImpl.Recognize上的Google.Api.Gax.Grpc.ApiCall2.Sync(TRequest请求,CallSettings perCallCallSettings)中的tryExtensions . <> c__DisplayClass1_0`2.b__0(请求请求,CallSettings callSettings)来自C:\ Users \ jorda \ Google Drive \ VSProjects \ Rc2Solver中的Rc2Solver.frmMain.RecognizeWordsGoogleSpeechApi()的Google.Cloud.Speech.V1.SpeechClient.Recognize(RecognitionConfig config,RecognitionAudio audio,CallSettings callSettings)中的RecognizeRequest请求,CallSettings callSettings) \ 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

    事实证明,问题是需要使用指定的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");
      }
     });
    

相关问题