'How to get authentication for Using Googles Media Translation API with Springboot

I am trying out Google's Media Translation API. I cloned their test code that can be found here

but I am getting an error 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.

However, I already have the JSON file downloaded so I want to know how I can make the library point to that file as the source of authentication.

I have tried export GOOGLE_APPLICATION_CREDENTIALS=/Users/tobilobaowolabi/Downloads/blood-drive-296909-03dad529f7b9.json but it is not working either



Solution 1:[1]

You may point your library to your source of authentication through your code.

First you need to update your pom.xml file and add below code inside the <dependencies>

<dependency>
      <groupId>com.google.auth</groupId>
      <artifactId>google-auth-library-oauth2-http</artifactId>
      <version>1.3.0</version>
</dependency>

Once done with pom.xml, go to your java code and add below code inside translateFromFile method.

String jsonPath = "<your_json_file_path>";
    GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath))
    .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));

 SpeechTranslationServiceSettings speechTranslationServiceSettings =
     SpeechTranslationServiceSettings.newBuilder()
         .setCredentialsProvider(FixedCredentialsProvider.create(credentials))
         .build();

The above code is to call your json path as credentials and then build it through SpeechTranslationServiceSettings.

Lastly, you need to pass SpeechTranslationServiceSettings to the create() of SpeechTranslationServiceClient .

try (SpeechTranslationServiceClient client = SpeechTranslationServiceClient.create(speechTranslationServiceSettings))

You may check this Authentication Sample Code as reference for the authentication of credentials.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1