简体   繁体   中英

Error: 7 PERMISSION_DENIED: Your application has authenticated using end user credentials from the Google Cloud SDK

This was working a couple months ago without code changes inside of my websocket server, however using it today it seems that the Google speech to text api no longer allows authentication using access tokens.

This was my previously working method until I hit this error today

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

That nets me the above error in the title.

I also tried switching to a service account (which I used in the past) by setting up the environment as follows

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

I then run the client without the above code and instead run:

const client = new speech.SpeechClient();

and that nets me this beautiful error instead, even though the environment is set at this point with the project Id

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

Any help in resolving this would be much appreciated!

I resolved the environment problem and subsequent error by doing the following:

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

const client = new speech.SpeechClient(options);

I was able to follow the Official Quickstart and got it working by using Client Libraries with no issues. I will explain what I did right below.

From Cloud Speech-to-Text - Quickstart :

  1. Create or select a project:

    gcloud config set project YOUR_PROJECT_NAME

  2. Enable the Cloud Speech-to-Text API for the current project:

    gcloud services enable speech.googleapis.com

  3. Create a service account:

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

  4. Download a private key as JSON:

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

  5. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the JSON file that contains your service account key:

    export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"


  1. Install the Client Library

    npm install --save @google-cloud/speech

  2. Created a quickstart.js file and put the following code sample inside:

    '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' is the path where your test.raw audio is located.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM