[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

authenticated for google cloud environment but text to speech not working #5096

Closed
idodekerobo opened this issue Feb 28, 2024 · 3 comments
Closed
Assignees
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: question Request for information or clarification. Not an issue.

Comments

@idodekerobo
Copy link
idodekerobo commented Feb 28, 2024

when i instantiate gCloudStorage and call the loadCredentials function - i'm seeing my storage buckets so it seems i'm authenticated, but when i call google cloud text to speech in my application code, it says "Error: Could not load the default credentials".

i also get the same error when i try to do something simple like, output voices using googleTextToSpeechClient.listVoices({ languageCode }).

it only works when i directly add a reference to my service account directly to my .env file like this GOOGLE_APPLICATION_CREDENTIALS=./firebase-service-account-key.json

would appreciate any help or feedback! i currently can't seem to get text to speech to work in app any other way.

i've tried the following:

  • passing in the service account fields to directly into the credentials object of the initializer of firebase or cloud storage instance
  • putting the actual json directly into my .env file and base64 encoding it
  • double checked that my credentials are enabled with the Cloud Text-to-Speech API in the console of my project

Environment details

  • which product (packages/*): "@google-cloud/storage": "^7.7.0" "@google-cloud/text-to-speech": "^5.1.0",
  • OS: Mac OS Sonoma
  • Node.js version: v18.17.1
  • npm version: 9.6.7
  • google-cloud-node version: "@google-cloud/storage": "^7.7.0" "@google-cloud/text-to-speech": "^5.1.0",

Steps to reproduce

loadCredentials() returns the expected storage buckets and firebase works, but using the actual text to speech api isn't.

// firebase
initializeApp({
  credential: cert({
    type: process.env.FB_CERT_CREDENTIAL_TYPE,
    project_id: process.env.FB_CERT_PROJECT_ID,
    private_key_id: process.env.FB_CERT_PRIVATE_KEY_ID,
    private_key: process.env.FB_CERT_PRIVATE_KEY.replace(/\\n/g, '\n'),
    client_email: process.env.FB_CERT_CLIENT_EMAIL,
    client_id: process.env.FB_CERT_CLIENT_ID,
    auth_uri: process.env.FB_CERT_AUTH_URI,
    token_uri: process.env.FB_CERT_TOKEN_URI,
    auth_provider_x509_cert_url: process.env.FB_CERT_AUTH_PROVIDER_CERT_URL,
    client_x509_cert_url: process.env.FB_CERT_CLIENT_CERT_URL,
    universe_domain: process.env.FB_CERT_UNIVERSE_DOMAIN
  }),
  storageBucket: 'palate-d1218.appspot.com'
});

const base64EncodedServiceAccount = process.env.CLOUD_CREDENTIALS;
const decodedServiceAccount = Buffer.from(base64EncodedServiceAccount, 'base64').toString('utf-8');
const credentials = JSON.parse(decodedServiceAccount);

// google cloud storage
const gCloudStorage = new Storage({
   projectId: `palate-d1218`,
   keyFilename: "",
   credentials: credentials
})

async function loadCredentials() {
   console.log(gCloudStorage.authClient)
   try {
      const [buckets] = await gCloudStorage.getBuckets();
      console.log(`buckets: `)
      for (const bucket of buckets) {
         console.log(` - ${bucket.name}`)
      }
      console.log(`listed all buckets`)
   } catch (e) {
      console.log(`error getting buckets`)
      console.log(e)
   }
   
}
loadCredentials()

elsewhere in my application, calling either of the snippets below do not work and result in the below error.

module.exports.getVoices = async (req, res) => {
   const languageCode = 'en';
   const textToSpeech = require("@google-cloud/text-to-speech");
   const googleTextToSpeechClient = new textToSpeech.TextToSpeechClient();
   try {
      const [result] = await googleTextToSpeechClient.listVoices({ languageCode })
      res.send(result);
   } catch (e) {
      console.log(e);
      res.status(400).send(e);
   }
}
module.exports.transcribeTextFunction = async (palate) => {
   const { id, title, text } = palate
   const googleTextToSpeechClient = new TextToSpeechLongAudioSynthesizeClient()
   const bucketName = "gs://palate-d1218.appspot.com/"
   const gsUtilUriPath = `${bucketName}${title}.wav`

   try {
      const request = {
         input: { 
            text: text
         },
         audioConfig: {
            audioEncoding: `LINEAR16`
         },
         voice: {
            languageCode: 'en-US',
            name: 'en-US-Neural2-D',
            // ssmlGender: 'NEUTRAL',
         }, 
         parent: `projects/928931080353/locations/us`,
         outputGcsUri: gsUtilUriPath
      };
      const response = await googleTextToSpeechClient.synthesizeLongAudio(request);
      return response
   } catch (e) {
      console.log(`error transcribing text`)
      console.log(e);
      return `failed to transcribe text: ${e}`
   }
}

error

Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
    at GoogleAuth.getApplicationDefaultAsync (/Users/idodekerobo/Documents/gCloudApp/node_modules/@google-cloud/text-to-speech/node_modules/google-auth-library/build/src/auth/googleauth.js:207:19)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async GoogleAuth.getClient (/Users/idodekerobo/Documents/gCloudApp/node_modules/@google-cloud/text-to-speech/node_modules/google-auth-library/build/src/auth/googleauth.js:624:17)
    at async GrpcClient._getCredentials (/Users/idodekerobo/Documents/gCloudApp/node_modules/@google-cloud/text-to-speech/node_modules/google-gax/build/src/grpc.js:145:24)
    at async GrpcClient.createStub (/Users/idodekerobo/Documents/gCloudApp/node_modules/@google-cloud/text-to-speech/node_modules/google-gax/build/src/grpc.js:308:23)
(node:95143) MetadataLookupWarning: received unexpected error = Invalid response from metadata service: incorrect Metadata-Flavor header. code = UNKNOWN
(Use `node --trace-warnings ...` to show where the warning was created)
@danielbankhead danielbankhead added type: question Request for information or clarification. Not an issue. priority: p2 Moderately-important priority. Fix may not be included in next release. labels Feb 28, 2024
@danielbankhead
Copy link
Member

i've tried the following:

  • passing in the service account fields to directly into the credentials object of the initializer of firebase or cloud storage instance
  • putting the actual json directly into my .env file and base64 encoding it

These two should work in addition to GOOGLE_APPLICATION_CREDENTIALS - do you mind trying the instructions in either of these two places?

@idodekerobo
Copy link
Author
idodekerobo commented Feb 29, 2024

this now works in my local environment using ADC. for my heroku hosted instance, i will try this solution in this stack overflow answer - it didn't work locally, but it sounds like it may work for my hosted app.
there's also options like this that leverage the google auth library that you shared.

@idodekerobo
Copy link
Author
idodekerobo commented Feb 29, 2024

@danielbankhead neither of those two solutions i linked prior seemed to work on my heroku instance.
it also didn't work hard coding by service account keys using .env variables in my heroku environment. i kept getting the same "could not load default credentials" on my heroku logs that i was locally before using ADC.

this stack overflow answer did work though and i now seem to be up and running.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

3 participants