python-speech-recognition icon indicating copy to clipboard operation
python-speech-recognition copied to clipboard

changing the key fails

Open jzoudavy opened this issue 7 years ago • 3 comments

I got a new GCP API key, and I tried to use it but I keep getting broken connection:

Traceback (most recent call last): File "main.py", line 13, in <module> speech=r.recognize_google(audio,key='A...........8g') File "/home/davy/anaconda3/lib/python3.6/site-packages/speech_recognition/__init__.py", line 845, in recognize_google raise RequestError("recognition connection failed: {}".format(e.reason)) speech_recognition.RequestError: recognition connection failed: [Errno 32] Broken pipe I don't really understand why that is. I noticed that in the init.py file the key is set as follows:

if key is None: key = "AIz...bla...bla..bla...gw" url = "http://www.google.com/speech-api/v2/recognize?{}".format(urlencode({ "client": "chromium", "lang": language, "key": key, }))

Do i need to change the client? but it is just an API key client shouldn't matter right?

jzoudavy avatar May 15 '18 03:05 jzoudavy

Hi @jzoudavy ,

You might have gotten a key for the Google Cloud Speech API, which is different from the Google Web Speech API that .recognize_google() uses. There is an alternative recognizer in the SpeechRecognition package for using the Google Cloud APIs. That is the .recognize_google_cloud() method (docs here). In that case, you actually store the key in a JSON file.

somacdivad avatar May 15 '18 12:05 somacdivad

Hi

Thanks for answering my questions.

The credentials_json it takes a string right? so i should just read the json file as a string and pass it on? The json.load method returns dicts and it seems the method doesn't like that.

jzoudavy avatar May 16 '18 04:05 jzoudavy

@jzoudavy Sorry for delayed response. You are correct, credentials_json takes a string, but json.load() returns a dict. If you use json.loads(), you will get a string and it should work. Hope that helps!

For more info on working with JSON data, check out Real Python's guide to Working with JSON Data in Python.

Let me know how it works for you!

somacdivad avatar May 21 '18 15:05 somacdivad