首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通过服务帐户创建的无法使用的实例-身份验证错误

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

Stack Overflow用户
提问于 2017-07-26 22:04:00
回答 1查看 297关注 0票数 0

我遵循了Google的语音API的快速启动文档,以便为帐户启用计费和API。此帐户已授权服务帐户代表它创建计算实例。在子帐户上创建一个实例,托管一个二进制文件以使用语音API之后,我无法在C#语音示例中成功地使用谷歌提供的示例C#代码:

代码语言:javascript
复制
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实例时使用的范围:

代码语言:javascript
复制
credential = new ServiceAccountCredential(
                new ServiceAccountCredential.Initializer(me)
                {
                    Scopes = new[] { ComputeService.Scope.Compute, ComputeService.Scope.CloudPlatform }
                }.FromPrivateKey(yk)

                );

我在网上没有发现任何关于为服务帐户参与者专门授权或验证语音API的信息或代码。任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

发布于 2017-07-27 19:57:04

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

代码语言:javascript
复制
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");
  }
 });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45338194

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档