Google-cloud-python: Speech: 403 Request had insufficient authentication scopes.

Created on 8 Mar 2018  路  6Comments  路  Source: googleapis/google-cloud-python

See #3212 for more information

Getting the same error and also it gave me 403 request error.
google.api_core.exceptions.PermissionDenied: 403 Request had insufficient authentication scopes.

Note: Google SPeech API is enabled and I am using Python 3.6

Here is the code, thank you for your help.

 import argparse
 import io

 from google.oauth2 import service_account

 scope = 'https://www.googleapis.com/auth/drive'
 credentials = service_account.Credentials.from_service_account_file('Speech2Text-
 71ae27307424.json',scopes=(scope,))

 def transcribe_file_with_word_time_offsets(speech_file):
     """Transcribe the given audio file synchronously and output the word time
     offsets."""
     print("Start")

     from google.cloud import speech
     from google.cloud.speech import enums
     from google.cloud.speech import types   

     print("checking credentials")

     client = speech.SpeechClient(credentials=credentials)

     print("Checked")
     with io.open(speech_file, 'rb') as audio_file:
          content = audio_file.read()

     print("audio file read")    

     audio = types.RecognitionAudio(content=content)

     print("config start")
     config = types.RecognitionConfig(
             encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
             sample_rate_hertz=16000,
             language_code='en-US',
             enable_word_time_offsets=True)

     print("Recognizing:")
     response = client.recognize(config, audio) ## MAJOR PROBLEM
     print("Recognized")

     for result in response.results:
         alternative = result.alternatives[0]
         print('Transcript: {}'.format(alternative.transcript))

         for word_info in alternative.words:
             word = word_info.word
             start_time = word_info.start_time
             end_time = word_info.end_time
             print('Word: {}, start_time: {}, end_time: {}'.format(
                 word,
                 start_time.seconds + start_time.nanos * 1e-9,
                 end_time.seconds + end_time.nanos * 1e-9))

 def transcribe_gcs_with_word_time_offsets(gcs_uri):
     """Transcribe the given audio file asynchronously and output the word time
     offsets."""
     from google.cloud import speech
     from google.cloud.speech import enums
     from google.cloud.speech import types
     client = speech.SpeechClient()

     audio = types.RecognitionAudio(uri=gcs_uri)
     config = types.RecognitionConfig(
         encoding=enums.RecognitionConfig.AudioEncoding.FLAC,
         sample_rate_hertz=16000,
         language_code='en-US',
         enable_word_time_offsets=True)

     operation = client.long_running_recognize(config, audio)

     print('Waiting for operation to complete...')
     result = operation.result(timeout=90)

     for result in result.results:
         alternative = result.alternatives[0]
         print('Transcript: {}'.format(alternative.transcript))
         print('Confidence: {}'.format(alternative.confidence))

         for word_info in alternative.words:
             word = word_info.word
             start_time = word_info.start_time
             end_time = word_info.end_time
             print('Word: {}, start_time: {}, end_time: {}'.format(
                 word,
                 start_time.seconds + start_time.nanos * 1e-9,
                 end_time.seconds + end_time.nanos * 1e-9))

 if __name__ == '__main__':
     parser = argparse.ArgumentParser(description=__doc__,
         formatter_class=argparse.RawDescriptionHelpFormatter)
     parser.add_argument(dest='path', help='File or GCS path for audio file to be recognized')
     args = parser.parse_args()
     if args.path.startswith('gs://'):
         transcribe_gcs_with_word_time_offsets(args.path)
     else:
         transcribe_file_with_word_time_offsets(args.path)
question speech

Most helpful comment

You can't use the drive scope to access Cloud Speech, you need to use to google-cloud-platform scope, or just leave scope unspecified and the library will handle it.

All 6 comments

You can't use the drive scope to access Cloud Speech, you need to use to google-cloud-platform scope, or just leave scope unspecified and the library will handle it.

@sohamsil I'm not sure if you saw this, but this was the answer to your question. Please feel free to ask if you have any more questions.

@jonparrott @chemelnucfin Thanks a lot for the help. Removing the scope helped and the code executes fine on my machine.

However, when I tried to run the same on Cloudshell it gave an invalid_scope error. I believe it requires the google-cloud-platform scope, correct me if I am wrong.
I tried the scope as https://www.googleapis.com/auth/cloud-platform but it's showing invalid_scope.

Note: After running pip install -U google-cloud-speech in cloud shell, it gave me the below exception.

Installing collected packages: google-api-core, google-cloud-speech
  Found existing installation: google-api-core 0.1.4
    Uninstalling google-api-core-0.1.4:
Exception:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/pip-9.0.1-py2.7.egg/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/local/lib/python2.7/dist-packages/pip-9.0.1-py2.7.egg/pip/commands/install.py", line 342, in run
    prefix=options.prefix_path,
  File "/usr/local/lib/python2.7/dist-packages/pip-9.0.1-py2.7.egg/pip/req/req_set.py", line 778, in install
    requirement.uninstall(auto_confirm=True)
  File "/usr/local/lib/python2.7/dist-packages/pip-9.0.1-py2.7.egg/pip/req/req_install.py", line 754, in uninstall
    paths_to_remove.remove(auto_confirm)
  File "/usr/local/lib/python2.7/dist-packages/pip-9.0.1-py2.7.egg/pip/req/req_uninstall.py", line 115, in remove
    renames(path, new_path)
  File "/usr/local/lib/python2.7/dist-packages/pip-9.0.1-py2.7.egg/pip/utils/__init__.py", line 267, in renames
    shutil.move(old, new)
  File "/usr/lib/python2.7/shutil.py", line 303, in move
    os.unlink(src)
OSError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/google/api_core/__init__.py'

@jonparrott It worked in the cloud shell as well. You're right! I had missed out on creating a virtualenv while following the tutorials before this. Thank you :)

replace this line:

static string[] Scopes = { SheetsService.Scope.Spreadsheets.ReadOnly };

with this:

static string[] Scopes = { SheetsService.Scope.Spreadsheets };

and if you did not pass, delete (token.json) folder and try again

Was this page helpful?
0 / 5 - 0 ratings