Google Cloud Speech API创建API Key

创建api key

1.在Credentials界面,点击Create Credentials后在下拉菜单中选择API Key




2.弹出Create a new key的弹框


输入key的名字,如果仅仅用于测试,Restriction那一栏可以留白,但是如果打算在项目中部署,需设置restriction



3.点击创建,API key created弹框将会显示出你最新创建出来的key



4.受限制的API key(非测试apk需要)

默认的API key是不受限的,任何人可以读取或者访问存放这个key的手机,这样是不安全的。我们建议你用一个受限的API key来限制使用。


添加受限的API key,点击API key create,弹出API key配置界面


受限的类型取决于你的应用的需要:

.web项目选择HTTP referrers

.不能支持服务账号的后端应用选择IP addresses

.android应用选择Android apps

.ios应用选择ios apps


测试的时候需要使用非受限的API key,但是建议添加受限的API key或者删除这个API key当你发布应用或产品后










参考资料:https://cloud.google.com/speech/

https://cloud.google.com/speech/docs/common/auth#service-accounts

https://developers.google.com/identity/protocols/application-default-credentials

http://io2015codelabs.appspot.com


### 集成 Google Cloud Speech-to-Text API 到 Koa2 为了在 Koa2 应用程序中集成 Google Cloud Speech-to-Text API,需遵循几个重要步骤来设置环境、配置依赖项以及编写必要的代码逻辑。 #### 设置项目结构和安装依赖包 首先,在本地环境中创建一个新的 Node.js 项目,并初始化 `package.json` 文件。接着,通过 npm 安装所需的库: ```bash npm init -y npm install koa @google-cloud/speech axios form-data ``` 这会引入 Koa 框架作为服务器基础架构[@google-cloud/speech](https://www.npmjs.com/package/@google-cloud/speech) 是官方提供的 SDK 来访问 Google 的语音识别服务;而 `axios` 和 `form-data` 可帮助处理 HTTP 请求与文件上传操作[^1]。 #### 创建应用程序入口文件 建立名为 `app.js` 或者其他名称的应用启动脚本,在其中定义基本路由和服务行为: ```javascript// app.js const Koa = require('koa'); const Router = require('@koa/router'); const bodyParser = require('koa-bodyparser'); const app = new Koa(); const router = new Router(); router.post('/transcribe', async (ctx, next) => { const { audioContent } = ctx.request.body; try { let transcriptionResult = await transcribeAudio(audioContent); ctx.response.status = 200; ctx.response.body = JSON.stringify({ message: "Transcription successful", result: transcriptionResult }); } catch(error){ console.error(`Error during transcription process ${error}`); ctx.throw(500,'Failed to perform speech recognition.'); } }); async function transcribeAudio(contentString){ // Initialize a client const speechClient = new SpeechClient({ keyFilename: 'path/to/keyfile.json' // Replace with your service account file path }); const request = { config: { encoding: 'LINEAR16', sampleRateHertz: 16000, languageCode: 'en-US' }, audio: { content: Buffer.from(contentString, 'base64').toString('binary') } }; const [response] = await speechClient.recognize(request); return response.results.map(result => result.alternatives[0].transcript).join('\n'); } app.use(bodyParser()); app.use(router.routes()).use(router.allowedMethods()); module.exports = app; if (!module.parent) { const port = process.env.PORT || 3000; app.listen(port, () => { console.log(`Server running on http://localhost:${port}/`); }); } ``` 上述代码片段展示了如何构建一个简单的 POST 接口 `/transcribe` ,它接受包含音频数据的请求体参数 `audioContent`. 当接收到有效载荷后,调用辅助函数 `transcribeAudio()` 将其转换为文字形式并返回给客户端. 请注意替换 `'path/to/keyfile.json'` 为你自己的 GCP 凭证路径,并确保该账户具有足够的权限去调用 Speech-to-Text API ^[1].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值