Python调用Google Cloud Speech API遇SSL证书验证失败问题求助
I'm using the SpeechRecognition library with this code:
import speech_recognition as sr AUDIO_FILE = 'test_audio.wav' with open("api-key.json") as f: GOOGLE_CLOUD_SPEECH_CREDENTIALS = f.read() r = sr.Recognizer() with sr.AudioFile(AUDIO_FILE) as source: audio = r.record(source) print('Starting recognition...') print(r.recognize_google_cloud(audio, credentials_json=GOOGLE_CLOUD_SPEECH_CREDENTIALS)) print('Completed')
When I run it, I hit this error:
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)
Both the audio file and API key file are properly in place and accessible.
Hey there, that SSL certificate error is a super common snag when working with Python API calls—especially on macOS or environments where system root certificates aren't configured correctly. Let's walk through some fixes that should get you back on track:
1. Update Root Certificates with certifi
The simplest fix is to use the certifi library, which provides Mozilla's curated set of trusted root certificates. Here's how:
First, install certifi if you haven't already:
pip install certifi
Then tweak your code to tell the underlying requests library (which SpeechRecognition uses) to use certifi's certificates:
import speech_recognition as sr import ssl import certifi AUDIO_FILE = 'test_audio.wav' with open("api-key.json") as f: GOOGLE_CLOUD_SPEECH_CREDENTIALS = f.read() # Use certifi's verified certificate bundle ssl._create_default_https_context = lambda: ssl.create_default_context(cafile=certifi.where()) r = sr.Recognizer() with sr.AudioFile(AUDIO_FILE) as source: audio = r.record(source) print('Starting recognition...') print(r.recognize_google_cloud(audio, credentials_json=GOOGLE_CLOUD_SPEECH_CREDENTIALS)) print('Completed')
2. Fix macOS Python Certificate Gaps
If you're on macOS using the system-installed Python, Apple often skips including the full root certificate bundle. Run this terminal command to install the missing certificates:
/Applications/Python\ 3.X/Install\ Certificates.command
Just replace 3.X with your actual Python version (like 3.9 or 3.10).
3. Temporary Insecure Workaround (For Testing Only)
If you just need to test quickly and don't care about security (never use this in production), you can disable SSL verification entirely:
import speech_recognition as sr import ssl # Disable SSL checks (insecure—only for testing!) ssl._create_default_https_context = ssl._create_unverified_context AUDIO_FILE = 'test_audio.wav' with open("api-key.json") as f: GOOGLE_CLOUD_SPEECH_CREDENTIALS = f.read() r = sr.Recognizer() with sr.AudioFile(AUDIO_FILE) as source: audio = r.record(source) print('Starting recognition...') print(r.recognize_google_cloud(audio, credentials_json=GOOGLE_CLOUD_SPEECH_CREDENTIALS)) print('Completed')
4. Check Network/Proxy Settings
If you're behind a corporate proxy, your network might be intercepting SSL traffic. Set these environment variables before running your script to route traffic through your proxy:
export HTTP_PROXY=http://your-proxy-address:port export HTTPS_PROXY=http://your-proxy-address:port
Most of the time, updating certificates with certifi or running the macOS certificate install command will fix the issue. Give them a shot!
内容的提问来源于stack exchange,提问作者Shanid

