Google Cloud Translator .net客户端 [英] Google cloud translator .net client
问题描述
我正在尝试在.net项目中使用Google Translator API.我已经安装了这个库
I am trying to use google translator api in my .net project. I have installed this library
安装包Google.Cloud.Storage.V1
Install-Package Google.Cloud.Storage.V1
在我的代码中,我正在尝试
In my code I am trying this
Console.OutputEncoding = System.Text.Encoding.Unicode;
TranslationClient client = TranslationClient.Create();
var response = client.TranslateText("Hello World.", "ru");
Console.WriteLine(response.TranslatedText);
我收到验证错误,我真的很困惑该怎么做.我不能按原样将api密钥传递给create函数吗?还是一个问题?
I am getting authentication error, I am really confused how to do that. Can't I just pass an api key into the create function as it is? Or is that an issue?
我看到Create函数有一个选项
I see that Create function has an option
GoogleCredential.FromJson(parmas)
但是我可以在其中传递一个json字符串吗?如果是的话,该JSON的格式应该是什么?
But can I pass a json string as it in there? And if yes, what should be the format of that JSON?
先谢谢了.
推荐答案
以下是示例应用程序:
https://github.com/GoogleCloudPlatform/dotnet-docs-samples
Here are sample applications:
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
First install NuGet package:
PM> install-package Google.Cloud.Translation.V2 -pre
这是为我工作的绝对最简单的代码.3行代码,还不错:)
Here is the absolute simplest code that worked for me. 3 lines of code, not bad :)
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);
这篇关于Google Cloud Translator .net客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!