You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Node.js调用Speech-to-Text v2 API报404错误求助

问题:调用Speech-to-Text v2 REST API返回404错误

尝试用Node.js向Speech-to-Text v2 REST API发送POST请求时,始终收到Error: HTTP error! status: 404错误,使用了官方文档提供的服务端点,求排查问题。

错误原因分析

你的请求URL格式错误,这是导致404的核心问题:

  • 原URL中错误添加了recognizer=前缀,正确的端点路径应该直接以projects/开头,不需要额外的recognizer=字段。
  • 另外注意你的projectId变量值写了my_project_idr,多了一个末尾的r,需要修正为实际的项目ID。

修正后的代码

const fs = require('fs');
const fetch = require('node-fetch');

const apiKey = '你的API密钥';
const projectId = '你的项目ID'; // 注意修正此处的拼写错误
const location = 'us-central1';
const model = 'chirp';

const audioFilePath = 'audios/my_audio.mp3';
const audioContent = fs.readFileSync(audioFilePath).toString('base64');

const requestBody = {  
  config: { 
    language_codes: ["en-US"],
    model: "chirp",
    features: {
      "enableWordTimeOffsets": true,
      "enableWordConfidence": true    
    },
  },
  content: audioContent
};

const requestOptions = {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    Authorization: `Bearer ${apiKey}`,
  },
  body: JSON.stringify(requestBody),
};

// 修正后的API端点URL
fetch(`https://us-central1-speech.googleapis.com/v2/projects/${projectId}/locations/${location}/recognizers/_:recognize`, requestOptions)
.then(response => {    
  if (!response.ok) {      
    throw new Error(`HTTP error! status: ${response.status}`);    
  }    
  return response.json();  
})  
.then(data => {    
  console.log(data);  
})  
.catch(error => {    
  console.error('Error:', error.message);  
});

额外注意事项

  • 确保你的API密钥拥有Speech-to-Text API的调用权限,并且项目已启用该API。
  • 确认音频文件路径正确,文件格式符合API要求(MP3格式是支持的,但需确保编码正确)。

内容的提问来源于stack exchange,提问作者seriously

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 08:33:35