【问题标题】:Error: 7 PERMISSION_DENIED: Your application has authenticated using end user credentials from the Google Cloud SDK错误:7 PERMISSION_DENIED:您的应用程序已使用来自 Google Cloud SDK 的最终用户凭据进行身份验证
【发布时间】:2020-01-23 01:04:06
【问题描述】:

几个月前这在我的 websocket 服务器内部没有代码更改的情况下工作,但是今天使用它似乎 Google 语音到文本 api 不再允许使用访问令牌进行身份验证。

这是我以前的工作方法,直到我今天遇到这个错误

const client = new speech.SpeechClient({
   access_token: ACCESS_TOKEN,
   projectId: 'project-name'
});

这让我在标题中出现了上述错误。

我还尝试通过如下设置环境来切换到服务帐户(我过去使用过)

export GOOGLE_APPLICATION_CREDENTIALS="path-to-key.json"

然后我在没有上述代码的情况下运行客户端,而是运行:

const client = new speech.SpeechClient();

这反而给我带来了这个美丽的错误,即使此时环境是使用项目 ID 设置的

Error: Unable to detect a Project Id in the current environment.

任何帮助解决此问题将不胜感激!

【问题讨论】:

标签: javascript sockets oauth gcloud google-speech-api


【解决方案1】:

我通过执行以下操作解决了环境问题和后续错误:

const options = {
  keyFilename: 'path-to-key.json',
  projectId: 'project-name',
};

const client = new speech.SpeechClient(options);

【讨论】:

  • 感谢您发布您的解决方案作为答案!
  • 没问题,如果遇到类似情况,希望确保帮助其他人:)
【解决方案2】:

我能够遵循官方快速入门并通过使用客户端库使其正常工作。我将在下面解释我做了什么。

来自Cloud Speech-to-Text - Quickstart

  1. 创建或选择一个项目:

    gcloud config set project YOUR_PROJECT_NAME

  2. 为当前项目启用 Cloud Speech-to-Text API:

    gcloud services enable speech.googleapis.com

  3. 创建服务帐号:

    gcloud iam service-accounts create [SA-NAME] \ --description "[SA-DESCRIPTION]" \ --display-name "[SA-DISPLAY-NAME]"

  4. 以 JSON 格式下载私钥:

    gcloud iam service-accounts keys create ~/key.json \ --iam-account [SA-NAME]@[PROJECT-ID].iam.gserviceaccount.com

  5. 将环境变量 GOOGLE_APPLICATION_CREDENTIALS 设置为包含您的服务帐号密钥的 JSON 文件的文件路径:

    export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"


  1. 安装客户端库

    npm install --save @google-cloud/speech

  2. 创建了一个quickstart.js 文件并将以下代码示例放入其中:

    'use strict';

    // [START speech_quickstart] async function main() { // Imports the Google Cloud client library const speech = require('@google-cloud/speech'); const fs = require('fs');

    // Creates a client const client = new speech.SpeechClient();

    // The name of the audio file to transcribe const fileName = './resources/audio.raw';

    // Reads a local audio file and converts it to base64 const file = fs.readFileSync(fileName); const audioBytes = file.toString('base64');

    // The audio file's encoding, sample rate in hertz, and BCP-47 language code const audio = { content: audioBytes, }; const config = { encoding: 'LINEAR16', sampleRateHertz: 16000, languageCode: 'en-US', }; const request = { audio: audio, config: config, };

    // Detects speech in the audio file const [response] = await client.recognize(request); const transcription = response.results .map(result => result.alternatives[0].transcript) .join('\n'); console.log("Transcription: ${transcription}"); } main().catch(console.error);

WHERE const fileName = './resources/audio.raw' 是您的 test.raw 音频所在的路径。

【讨论】:

  • 我之前已经按照这些步骤进行了操作,但同样的问题是环境设置不正确。经过大量挖掘后,我通过执行 ` const options = { keyFilename: 'path-to-key.json', projectId: 'project-name', }; 解决了这个问题const client = new speech.SpeechClient(options); ` 我会关闭它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-27
  • 2020-07-13
  • 2021-10-15
  • 2022-01-27
  • 1970-01-01
相关资源
最近更新 更多