无法加载默认凭据

时间:2017-01-06 12:55:08

标签: node.js google-cloud-platform google-search-api

我正在尝试使用带有nodejs的google语音API。要通过代码访问API,我们需要对自己进行身份验证。我正在使用默认凭据以便对自己进行身份验证。以下链接显示如何使用默认凭据进行身份验证。

https://developers.google.com/identity/protocols/application-default-credentials#calling

我已经设置了上面链接中提到的环境变量,但我仍然收到错误“无法加载默认凭据。”

我正在使用以下链接中的代码来测试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;

这是我在上面分享的链接中的确切代码。

我使用以下命令来运行代码

node recognize sync ./resources/audio.raw

0 个答案:

没有答案