Unable to use Google's text-to-speech API (requested via service account) after hosting backend on Fly.io

34 Views Asked by At

I have built a webapp with a backend expressJS server that uses Google's text-to-speech API (which the app requests via a service account) to convert text to speech.

Code that converts text to speech:

const textToSpeech = require("@google-cloud/text-to-speech");
const fs = require("fs");
const util = require("util");

const client = new textToSpeech.TextToSpeechClient({
  keyFilename: "./cheftalk-401015-2df6c845f5cf.json",
});

// function to synthesize speech
async function synthesizeSpeech(text) {
  const request = {
    input: { text },
    voice: {
      languageCode: "en-UK",
      name: "en-GB-News-L",
      ssmlGender: "MALE",
    },
    audioConfig: { audioEncoding: "MP3" },
  };
  const [response] = await client.synthesizeSpeech(request);
  const writeFile = util.promisify(fs.writeFile);
  await writeFile("output.mp3", response.audioContent, "binary");
  console.log("Audio content written to file: output.mp3");
}

module.exports = { synthesizeSpeech };

The reference .json file:

{
  "type": "service_account",
  "project_id": "###",
  "private_key_id": "###",
  "private_key": "###",
  "client_email": "###",
  "client_id": "###",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "###",
  "universe_domain": "googleapis.com"
}

I have tested the app in my local host node.js environment and this text-to-speech functionality works.

I have proceeded to host my backend onto Fly.io (with the above .json file included and hosted as well) and tested this text-to-speech functionality. However, this time the functionality does not work.

Appreciate if anyone who encountered a similar problem could share their solution - thanks in advance!

0

There are 0 best solutions below