1

I am trying to use Google Cloud Translation API. I have generated API key, but I don't have the .json file where are stored my credentials. Is there any way to put my API key in code so I can use Translation methods, or not? I have found only ways where it is needed to have .json file, but I don't have it. I have found a deprecated method :

Translate translate = TranslateOptions.newBuilder().setApiKey(API_KEY).build().getService();

but it shows me errors:

W/TranslateOptions: Ignoring Application Default Credentials {0}: using explicit setting for API key instead.
W/System.err: java.io.IOException: The Application Default Credentials are not available. They are available 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.
W/System.err:     at com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentials(DefaultCredentialsProvider.java:134)

Any help would be appreciated!

EDIT: When I put environment variable GOOGLE API KEY and wrote:

 Translate translate = TranslateOptions.getDefaultInstance().getService();
    Translation translation = translate.translate("¡Hola Mundo!");

I got error on translate.translate() line, it sends request like GET method, not like POST, and that's why it says that it is missing a valid API key, because it is not sending API key at all.

com.google.cloud.translate.TranslateException: The request is missing a valid API key.
newstudent
  • 65
  • 10
  • Please see https://stackoverflow.com/questions/50925060/ignoring-application-default-credentials-0-using-explicit-setting-for-api-key – JCompetence Sep 02 '21 at 11:49
  • @SusanMustafa Question is not helping me. Sorry – newstudent Sep 02 '21 at 11:52
  • print your environmental variable GOOGLE_API_KEY and see if there is a value there and if it is correct. If it is there, it means your API KEY is not correct... – JCompetence Sep 02 '21 at 13:05
  • How to do that? @SusanMustafa My API key is correct because in Postman my requests are accepted and the text is translated. – newstudent Sep 02 '21 at 13:07
  • https://stackoverflow.com/questions/318239/how-do-i-set-environment-variables-from-java/22315463#22315463 – JCompetence Sep 02 '21 at 13:12
  • https://stackoverflow.com/questions/7597058/android-setget-environmental-variables-in-java – JCompetence Sep 02 '21 at 13:12
  • @SusanMustafa I have printed the value in PowerShell. It is good – newstudent Sep 02 '21 at 13:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/236686/discussion-between-newstudent-and-susan-mustafa). – newstudent Sep 02 '21 at 13:18
  • @newstudent - you mean that request that is working with POST API is not working in Java API? – vitooh Sep 03 '21 at 07:52
  • @vitooh when I call method translate() it should be making POST request with parameter api key, but it is not, it is making GET method with no parameter, because it doesn't see my api key set anywhere. – newstudent Sep 03 '21 at 08:16
  • @newstudent - this might be worth to raise some bug, but I am not sure where it should be done. Can you add some link to reference of this function? – vitooh Sep 03 '21 at 11:35
  • @vitooh https://googleapis.dev/java/google-cloud-translate/latest/index.html – newstudent Sep 03 '21 at 13:52
  • There might be some bug in the Java API, as all language API's use REST under the hood. According to [the documentation](https://cloud.google.com/translate/docs/getting-support#file_bugs_or_feature_requests) it should be raised via "Send Feedback" button. If you are able to reproduce this issue you can raise it on this [page](https://cloud.google.com/translate/docs/reference/libraries/v2/java). – vitooh Sep 07 '21 at 10:36

1 Answers1

0

Create an environment variable : GOOGLE_API_KEY

The environment variable has your API key as a value.

Then do the below code:

  import com.google.cloud.translate.Translate;
  import com.google.cloud.translate.TranslateOptions;

  Translate translate = TranslateOptions.getDefaultInstance().getService();

-- Edited

How about this:

 HttpTransportOptions transportOptions = TranslateOptions.getDefaultHttpTransportOptions();
  transportOptions =
      transportOptions.toBuilder().setConnectTimeout(60000).setReadTimeout(60000).build();


  TranslateOptions translateOptions =
      TranslateOptions.newBuilder()
          .setApiKey(apiKey)
          .setTransportOptions(transportOptions)
          .build();


   Translate translate = translateOptions.getService();
JCompetence
  • 6,997
  • 3
  • 19
  • 26
  • But I have read: Service accounts are managed by IAM, and they represent non-human users. They are intended for scenarios where your application needs to access resources or perform actions on its own, such as running App Engine apps or interacting with Compute Engine instances. That is not what I need. User accounts are managed as Google Accounts, and they represent a developer, administrator, or any other person who interacts with Google Cloud. They are intended for scenarios where your application needs to access resources on behalf of a human user. And I have created user account... – newstudent Sep 02 '21 at 12:17
  • @newstudent Are you using basic or Cloud Translation advanced? IAM roles - You secure your translation requests by using Identity and Access Management. Create service accounts and grant them permissions by adding IAM roles. Cloud Translation - Advanced doesn't support API keys. – JCompetence Sep 02 '21 at 12:32
  • I am using just basic CloudTranslation. I want to translate messages, nothing more. I have created account and have API KEY. When I send POST request (https://translation.googleapis.com/language/translate/v2) in Postman it works. But I don't want to use HttpRequests in code, I want it to be easier and to use API methods. – newstudent Sep 02 '21 at 12:34