Beaudry Chase
Beaudry Chase

Reputation: 195

How does one create a TranslationServiceClient using a json as authentication?

Hello and thanks for reading my question.

I'm working on transitioning some code from Google.Cloud.Translation.V2 to Google.Cloud.Translate.V3 because we need to make use of the api advanced features. We are using a TranslationClient to get us the translations (from the V2 library) but we need to instead use the TranslationServiceClient (from the V3 library.) I'm having trouble instantiating a TranslationServiceClient with our credentials. The way to do it in V2 is straightforward:

TranslationClient.Create(GoogleCredential.FromJson("{\"the credentials\"}"));

From reading the documentation it is clear to me that to create a TranslationServiceClient without the default settings you need to use a TranslationServiceClientBuilder and give that the credentials. I couldn't find any examples, all of the code snippets use TranslationServiceClient.Create() which doesn't allow for any arguments.

Upvotes: 2

Views: 3093

Answers (2)

Sunghyun Cho
Sunghyun Cho

Reputation: 133

Positive attitude / embracing change: 

Problem solving / evolving:

const GOOGLE_TRANSLATE = new GoogleTranslate(
  {
    projectId: process.env.GOOGLE_PROJECT_ID,
    credentials: {
      client_email: auth.client_email,
      private_key: auth.private_key,
    }
  },
)

Upvotes: 0

Beaudry Chase
Beaudry Chase

Reputation: 195

Since the goal is to create a TranslationServiceClient using a json as authentication, one way to do this is as follows:

TranslationServiceClient client = new TranslationServiceClientBuilder {
                        JsonCredentials = "{\"the credentials\"}"
                    }.Build()

More info at https://cloud.google.com/docs/authentication/production#passing_the_path_to_the_service_account_key_in_code

Upvotes: 6

Related Questions