I am trying to use the google speech API with nodejs. For accessing the API through the code we need to authenticate ourselves. I am using the default credentials so as to authenticate myself. The following link shows how to authenticate using default credentials.
https://developers.google.com/identity/protocols/application-default-credentials#calling
I have set the environment variables as mentioned in the above link but still I am getting the error "Could not load the default credentials."
I am using the code in the following link so as to test the google speech api. https://cloud.google.com/speech/docs/rest-tutorial#run_the_sample
'use strict';
const Speech = require('@google-cloud/speech');
// [START speech_sync_recognize]
function syncRecognize (filename) {
// Instantiates a client
const speech = Speech();
const config = {
// Configure these settings based on the audio you're transcribing
encoding: 'LINEAR16',
sampleRate: 16000
};
// Detects speech in the audio file, e.g. "./resources/audio.raw"
return speech.recognize(filename, config)
.then((results) => {
const transcription = results[0];
console.log(`Transcription: ${transcription}`);
return transcription;
});
}
// [END speech_sync_recognize]
// [START speech_async_recognize]
function asyncRecognize (filename) {
// Instantiates a client
const speech = Speech();
const config = {
// Configure these settings based on the audio you're transcribing
encoding: 'LINEAR16',
sampleRate: 16000
};
// Detects speech in the audio file, e.g. "./resources/audio.raw"
// This creates a recognition job that you can wait for now, or get its result
// later.
return speech.startRecognition(filename, config)
.then((results) => {
const operation = results[0];
// Get a Promise represention the final result of the job
return operation.promise();
})
.then((transcription) => {
console.log(`Transcription: ${transcription}`);
return transcription;
});
}
// [END speech_async_recognize]
// [START speech_streaming_recognize]
const fs = require('fs');
function streamingRecognize (filename, callback) {
// Instantiates a client
const speech = Speech();
const options = {
config: {
// Configure these settings based on the audio you're transcribing
encoding: 'LINEAR16',
sampleRate: 16000
}
};
// Create a recognize stream
const recognizeStream = speech.createRecognizeStream(options)
.on('error', callback)
.on('data', (data) => {
console.log('Data received: %j', data);
callback();
});
// Stream an audio file from disk to the Speech API, e.g. "./resources/audio.raw"
fs.createReadStream(filename).pipe(recognizeStream);
}
// [END speech_streaming_recognize]
// [START speech_streaming_mic_recognize]
const record = require('node-record-lpcm16');
function streamingMicRecognize () {
// Instantiates a client
const speech = Speech();
const options = {
config: {
// Configure these settings based on the audio you're transcribing
encoding: 'LINEAR16',
sampleRate: 16000
}
};
// Create a recognize stream
const recognizeStream = speech.createRecognizeStream(options)
.on('error', console.error)
.on('data', (data) => process.stdout.write(data.results));
// Start recording and send the microphone input to the Speech API
record.start({ sampleRate: 16000 }).pipe(recognizeStream);
console.log('Listening, press Ctrl+C to stop.');
}
// [END speech_streaming_mic_recognize]
require(`yargs`)
.demand(1)
.command(
`sync <filename>`,
`Detects speech in an audio file.`,
{},
(opts) => syncRecognize(opts.filename)
)
.command(
`async <filename>`,
`Creates a job to detect speech in an audio file, and waits for the job to complete.`,
{},
(opts) => asyncRecognize(opts.filename)
)
.command(
`stream <filename>`,
`Detects speech in an audio file by streaming it to the Speech API.`,
{},
(opts) => streamingRecognize(opts.filename, () => {})
)
.command(
`listen`,
`Detects speech in a microphone input stream.`,
{},
streamingMicRecognize
)
.example(`node $0 sync ./resources/audio.raw`)
.example(`node $0 async ./resources/audio.raw`)
.example(`node $0 stream ./resources/audio.raw`)
.example(`node $0 listen`)
.wrap(120)
.recommendCommands()
.epilogue(`For more information, see https://cloud.google.com/speech/docs`)
.help()
.strict()
.argv;
This is the exact code that is there in the link that I have shared above.
I am using the following command to run the code
node recognize sync ./resources/audio.raw