mohsinali1317
mohsinali1317

Reputation: 4425

Google cloud translator .net client

I am trying to use google translator api in my .net project. I have installed this library

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);

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?

I see that Create function has an option

GoogleCredential.FromJson(parmas)

But can I pass a json string as it in there? And if yes, what should be the format of that JSON?

Thanks in advance.

Upvotes: 2

Views: 6654

Answers (2)

dragansr
dragansr

Reputation: 490

Here are sample applications:
https://github.com/GoogleCloudPlatform/dotnet-docs-samples

Authentication info:
https://cloud.google.com/docs/authentication/
https://cloud.google.com/docs/authentication/api-keys

First install NuGet package:
PM> install-package Google.Cloud.Translation.V2 -pre

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);

Upvotes: 10

Mayur Jaganmohan
Mayur Jaganmohan

Reputation: 31

Refer this link - https://developers.google.com/identity/protocols/application-default-credentials. Download .json file.

Install Google.Cloud.Translation.V2

string credential_path = @"D:\..\..\cred.json";
System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", credential_path);                                          

Upvotes: 3

Related Questions