1

To get the Text-to-Speech API working from a Python script on GCE, I tried using the following code running under GCE:

"""Synthesizes speech from the input string of text or ssml.

Note: ssml must be well-formed according to:
    https://www.w3.org/TR/speech-synthesis/
"""

# Authorize server-to-server interactions from Google Compute Engine.
import httplib2
from oauth2client.contrib import gce

credentials = gce.AppAssertionCredentials(
  scope='https://www.googleapis.com/auth/cloud-platform')
http = credentials.authorize(httplib2.Http())

from google.cloud import texttospeech

# Instantiates a client
client = texttospeech.TextToSpeechClient()

# Set the text input to be synthesized
synthesis_input = texttospeech.types.SynthesisInput(text="This is a test. It is only a test.")

# Build the voice request, select the language code ("en-US") and the ssml
# voice gender ("neutral")
voice = texttospeech.types.VoiceSelectionParams(
    language_code='en-US',
    ssml_gender=texttospeech.enums.SsmlVoiceGender.NEUTRAL)

# Select the type of audio file you want returned
audio_config = texttospeech.types.AudioConfig(
    audio_encoding=texttospeech.enums.AudioEncoding.MP3)

# Perform the text-to-speech request on the text input with the selected
# voice parameters and audio file type
response = client.synthesize_speech(synthesis_input, voice, audio_config)

# The response's audio_content is binary.
with open('output.mp3', 'wb') as out:
    # Write the response to the output file.
    out.write(response.audio_content)
    print('Audio content written to file "output.mp3"')

I expected a file, output.mp3, which would contain the speech for "This is a test. It is only a test."

I received: "PermissionDenied: 403 Request had insufficient authentication scopes."

I have used the You-Tube API and the authentication worked on that. What am I missing here?

mcgeo52
  • 51
  • 1
  • 1
  • 7

1 Answers1

2

As you are on a GCE instance try setting the access scopes for the instance to 'Allow full access to all Cloud APIs' and try again.

Also check that you are using the client library and service account key correctly as explained at [2]

[1]https://cloud.google.com/compute/docs/access/service-accounts?hl=en_US&_ga=2.55324139.-1189275507.1546438047#accesscopesiam

[2]https://cloud.google.com/text-to-speech/docs/reference/libraries

Germán A.
  • 193
  • 4
  • The only two choices that looked like they might work for 'Allow full access to all Cloud APIs' were: 1) 'https://www.googleapis.com/auth/cloud-platform' 2) 'https://www.googleapis.com/auth/compute' Neither worked. I have tried to follow the documentation for the service account key, but it does not match the reality of what happens. As much as I appreciate your response, i really need an answer, and it is nowhere that I can find it in the "documentation." – mcgeo52 Jan 18 '19 at 05:25
  • @ Germán A. do you have an answer? – mcgeo52 Jan 23 '19 at 04:02
  • 4
    I finally found the documentation that says 'https://www.googleapis.com/auth/cloud-platform' is what I need. I am assuming at this point the problem lies with the service account key. I have yet to find any documentation on service account key and SSH in a browser window. At least I know what I need to look for. Google has some of THE. WORST. DOCUMENTATION. EVER. – mcgeo52 Jan 23 '19 at 05:41
  • You may find information in regards to Service Account Keys at https://cloud.google.com/iam/docs/creating-managing-service-account-keys#getting_a_service_account_key and https://cloud.google.com/sdk/gcloud/reference/iam/service-accounts/keys/create. For the SSH browser documentation is available at https://cloud.google.com/compute/docs/ssh-in-browser – Germán A. Jan 23 '19 at 21:55