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

Speech: 403 Request had insufficient authentication scopes. #5006

Closed
chemelnucfin opened this issue Mar 8, 2018 · 6 comments
Closed

Speech: 403 Request had insufficient authentication scopes. #5006

chemelnucfin opened this issue Mar 8, 2018 · 6 comments
Assignees
Labels
api: speech Issues related to the Speech-to-Text API. type: question Request for information or clarification. Not an issue.

Comments

@chemelnucfin
Copy link
Contributor

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)
@chemelnucfin chemelnucfin added type: question Request for information or clarification. Not an issue. api: speech Issues related to the Speech-to-Text API. labels Mar 8, 2018
@theacodes
Copy link
Contributor

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.

@chemelnucfin
Copy link
Contributor Author

@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.

@sohamsil
Copy link

sohamsil commented Mar 9, 2018

@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'
 

@theacodes
Copy link
Contributor

@sohamsil you should use a virtualenv. See https://cloud.google.com/python/setup#installing_and_using_virtualenv

@sohamsil
Copy link

sohamsil commented Mar 10, 2018

@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 :)

@aliallamy
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: speech Issues related to the Speech-to-Text API. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

4 participants