I am trying to use GCP (Speech To Text service) in my spring boot application. However, I get the error:
public String recognizeVoice(String encodedfile) throws IOException
{
System.out.println("encoded file inside recognize Voice "+ encodedfile);
SpeechClient speechClient = SpeechClient.create(); // this line throws error
System.out.println("client created succesflly" );
// The path to the audio file to transcribe
String gcsUri = "src/main/resources/samplesSounds/punjabiAudioSpeech.wav";
// Builds the sync recognize request
RecognitionConfig config =
RecognitionConfig.newBuilder()
.setEncoding(RecognitionConfig.AudioEncoding.LINEAR16)
.setSampleRateHertz(16000)
.setLanguageCode("pa-Guru-IN")
.setModel("command_and_search")
.setAudioChannelCount(2)
.build();
RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(gcsUri).build();
// Performs speech recognition on the audio file
RecognizeResponse response = speechClient.recognize(config, audio);
List<SpeechRecognitionResult> results = response.getResultsList();
for (SpeechRecognitionResult result : results) {
// There can be several alternative transcripts for a given chunk of speech. Just use the
// first (most likely) one here.
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
System.out.printf("Transcription: %s%n", alternative.getTranscript());
}
return "FAILED";
}
Your default credentials were not found. To set up Application Default Credentials for your environment, see https://cloud.google.com/docs/authentication/external/set-up-adc.
at com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentials(DefaultCredentialsProvider.java:127) ~[google-auth-library-oauth2-http-1.22.0.jar:1.22.0]
at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:152) ~[google-auth-library-oauth2-http-1.22.0.jar:1.22.0]
at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:124) ~[google-auth-library-oauth2-http-1.22.0.jar:1.22.0]
at com.google.api.gax.core.GoogleCredentialsProvider.getCredentials(GoogleCredentialsProvider.java:70) ~[gax-2.42.0.jar:2.42.
I have tried setting up as a property in "application.properties " in spring boot. However, I am still getting this error. Property which I have set is: spring.cloud.gcp.credentials.location=key.json
They key.json file is generated through the service account in GCP. Is there anyother way I can access the services in cloud?
Most of the time, you do not need a service account key file. And it's a good news because this file is a security hole (you have a secret file that need to be handled like a secret)
On your local environment, you can use
gcloud auth application-default login
to use your own credentials (and roles)gcloud auth application-default login --impersonate-service-account=<sa-email>
to impersonate your service account and act as it (with its own roles. Useful to validate that the right roles are set)