我正在尝试在我的 .net 项目中使用 google translator api。我已经安装了这个库
Install-Package Google.Cloud.Storage.V1
在我的代码中我正在尝试这个
Console.OutputEncoding = System.Text.Encoding.Unicode;
TranslationClient client = TranslationClient.Create();
var response = client.TranslateText("Hello World.", "ru");
Console.WriteLine(response.TranslatedText);
我收到身份验证错误,我真的很困惑该怎么做。我不能直接将 api key 传递给 create 函数吗?或者这是一个问题?
我看到 Create 函数有一个选项
GoogleCredential.FromJson(parmas)
但是我可以在其中传递一个 json 字符串吗?如果是,该 JSON 的格式应该是什么?
提前致谢。
最佳答案
以下是示例应用程序:
https://github.com/GoogleCloudPlatform/dotnet-docs-samples
认证信息:
https://cloud.google.com/docs/authentication/
https://cloud.google.com/docs/authentication/api-keys
首先安装NuGet包:
PM> 安装包 Google.Cloud.Translation.V2 -pre
这是对我有用的绝对最简单的代码。 3 行代码,还不错 :)
var service = new TranslateService(new BaseClientService.Initializer { ApiKey = apiKeyTranslate });
var client = new TranslationClientImpl(service, TranslationModel.ServiceDefault);
var result = client.TranslateText("Hello, World", "sr");
Response.Write(result.TranslatedText);
关于c# - 谷歌云翻译.net客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44453841/