c# - 是否可以手动向 SpeechClient 提供 GoogleCredential(在 .NET API 中)?

标签 c# .net google-cloud-platform google-speech-api

我找到的所有 SpeechClient 文档都涉及在下载 SDK 后运行命令行,或者笨拙地设置“GOOGLE_APPLICATION_CREDENTIALS”环境变量以指向本地凭证文件。

我讨厌环境变量方法,而是想要一个从应用程序根目录加载共享的、源代码控制的开发帐户文件的解决方案。像这样:

var credential = GoogleCredential.FromStream(/*load shared file from app root*/);
var client = SpeechClient.Create(/*I wish I could pass credential in here*/);

有没有办法做到这一点,这样我就不必依赖环境变量了?

最佳答案

是的,通过将 GoogleCredential 转换为 ChannelCredentials,并使用它来初始化 Channel,然后将其包装在 >语音客户端:

using Grpc.Auth;

//...

GoogleCredential googleCredential;
using (Stream m = new FileStream(credentialsFilePath, FileMode.Open))
    googleCredential = GoogleCredential.FromStream(m);
var channel = new Grpc.Core.Channel(SpeechClient.DefaultEndpoint.Host,
    googleCredential.ToChannelCredentials());
var speech = SpeechClient.Create(channel);

更新 2018-02-02 https://cloud.google.com/docs/authentication/production现在展示了他们向 Google 云服务进行身份验证的所有可能方法,包括像这样的示例。

关于c# - 是否可以手动向 SpeechClient 提供 GoogleCredential(在 .NET API 中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42865917/

相关文章:

kubernetes - 用于 Google Kubernetes Engine 中 Stackdriver 日志记录的 JSON 格式

c# - XML 序列化不填充数组

node.js - 错误 :0909006C:PEM routines:get_name:no start line - for google cloud platform in heroku

docker - GCP : Unable to pull docker images from our GCP private container registry on ubuntu/debian VM instances

c# - 无缓冲的 XmlReader 实现

c# - 定制服务员有什么实际用途吗?

c# - 如何在不显式命名属性的情况下打印对象的所有属性值

c# - 异步/等待调用不返回。强制同步确实

C# 项目更新从两个表中选择的行的 MySQL 列表

c# - 64 位进程 Vista 中的 64 位 dll 失败 - 无法加载 DLL 'dllname' : The specified module could not be found