.NET:应用程序默认凭据不可用

.NET: The Application Default Credentials are not available

在我的应用程序中,我使用 Google 云翻译服务。

在我的开发机器上,我安装了 Google credentials 环境变量。

但是,在部署时,我的应用程序抛出以下错误:

The Application Default Credentials are not available.

The Environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined.

They are avaiable if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.

我以为凭据会自动编译到我的应用程序中。

这里可能是什么错误?

谢谢!

JSON 文件需要编译为嵌入式资源。

解决方法如下: http://www.hzhang.org/Umbraco/blog/use-google-cloud-translation-with-c/

在 C# 中使用 Google Cloud Translation

按照指南设置帐户。

将私钥下载为 JSON 并将其保存在文件夹 Assets 下(例如 API Project-xxxxxxx.json)。

重要提示:将其构建操作设置为嵌入式资源。

安装 NuGet 包 Google.Cloud.Translation.V2.

创建 TranslationClient

TranslationClient _tc;

GoogleCredential gc = GoogleCredential.FromStream(Utility.GetStreamFromResourceFile("API Project-xxxxxxx.json", GetType()));

_tc = TranslationClient.Create(gc);

您可以像下面这样将“要翻译的文本”从德语翻译成英语的示例进行翻译:

 string sTranslatedText = _tc.TranslateText("Text to be translated", "en", "de").TranslatedText;