使用Deepgram API转录Twilio音频时遭遇401 Unauthorized错误求助
我尝试用Deepgram API转录来自Twilio的音频文件,但在转录时遇到了401 Unauthorized错误。
以下是我的代码:
async def transcribe_audio(audio_url: str) -> dict: try: print(f"Sending audio to Deepgram for transcription: {audio_url}") response = requests.get(audio_url, auth=HTTPBasicAuth(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)) if response.status_code == 200: print("Successfully fetched the audio file from Twilio.") options = PrerecordedOptions( model="nova-2", smart_format=True, language="en", punctuate=True ) response = await deepgram_client.listen.rest.v("1").transcribe_url( {"url": audio_url}, options ) print(f"Deepgram Response: {response}") if response and hasattr(response, 'results'): transcript = response.results.channels[0].alternatives[0].transcript return { "transcription": transcript, "confidence": response.results.channels[0].alternatives[0].confidence } else: raise ValueError("No transcription results found in the response") else: raise ValueError(f"Failed to fetch audio from Twilio. HTTP Status Code: {response.status_code}") except Exception as e: print(f"Error during transcription: {e}") return {"error": str(e)}
出现问题的具体流程:
- 我能通过
requests.get方法成功从Twilio获取音频文件。 - 随后我通过
deepgram_client.listen.rest.v("1").transcribe_url方法将音频URL发送给Deepgram进行转录。 - 尽管Twilio的认证(
HTTPBasicAuth(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN))是正确的,但API调用还是返回了401 Unauthorized错误。
我收到的错误信息如下:
Error during transcription: DeepgramApiError: The remote server hosting the media returned a client error: 401 Unauthorized. (Status: 400)
我的环境:
- Twilio API:用于从Twilio获取音频
- Deepgram API:用于转录音频文件
我已经检查过我的API密钥,看起来没有问题,但还是遇到了这个问题。
备注:内容来源于stack exchange,提问作者D Batch
相关产品推荐
相关产品推荐

