I have written a Python script that uses the TextToSpeechClient from the google-cloud-texttospeech package. My script reads the authentication key file path from my config file and uses it to both set the environment (os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = config['api-keys']['google_credentials']) and for running the speech synthesis. This process works smoothly when on my laptop, but when on my target raspberry pi 4, I run into trouble; When it comes time to synthesize the text, this error is generated instead:
Error synthesizing speech: 401 Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project. [reason: "ACCESS_TOKEN_EXPIRED"
domain: "googleapis.com"
metadata {
key: "method"
value: "google.cloud.texttospeech.v1.TextToSpeech.SynthesizeSpeech"
}
metadata {
key: "service"
value: "texttospeech.googleapis.com"
}
]
This same "Expired" token works completely fine on my laptop.
This is my voice_synthesis.py module:
from google.cloud import texttospeech
import os
class VoiceSynthesizer:
def __init__(self, config):
self.client = texttospeech.TextToSpeechClient()
self.voice_settings = config['voice_settings']
self.google_credentials = config['api-keys']['google_credentials']
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = config['api-keys']['google_credentials']
def synthesize_speech(self, text):
synthesis_input = texttospeech.SynthesisInput(text=text)
voice = texttospeech.VoiceSelectionParams(
language_code="en-US",
name=self.voice_settings['voice_name']
)
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.LINEAR16,
pitch=self.voice_settings['pitch'],
speaking_rate=self.voice_settings['speaking_rate']
)
try:
response = self.client.synthesize_speech(
input=synthesis_input,
voice=voice,
audio_config=audio_config
)
print("Speech synthesized successfully")
return response.audio_content
except Exception as e:
print(f"Error synthesizing speech: {e}")
return None
def cleanup(self):
pass
I then tried generating a new key json file and moved it to my Raspberry Pi 4 via SCP. This key also causes the same error.
I then read someone's suggestion on a forum to make it and download it on the pi itself. So I installed Firefox, created an entirely new authentication section (just like my other one, full permissions), and downloaded the json. This time I didn't even move the file I just provided the path to the newly downloaded json file. It reads in the file just fine but when it comes time to run, it causes the same error.
I have also exported it to the environment variable as well.
I don't know if I'm missing a module or package for pip, or if I'm missing something more obvious as this seems like more people would have this problem running it on the pi.
All these same tokens run and work completely fine on my laptop with no issue.
"Verify that the date, time, and time zone are correct on the Pi." – John Hanley
I already did so... but not clearly enough. The date and time were the exact same, except the year was 2023, and today is now the 4th day of 2024. The New Year got me. With the updated time everything is now functioning. Thank you!