node使用@google-cloud/translate调用google翻译api,How set API KEY in Google Translate Node.js code
装包:
yarn add @google-cloud/translate
const { Translate } = require('@google-cloud/translate').v2
let googleKey = 'xxx'
let projectId = 'xxx'
const translate = new Translate({ projectId, key: googleKey })
const chatGoogleTranslateOnAzure = async (req, res) => {
let { text = '你好', target = 'en', apiKey = 'sk-xxx' } = req.body
if (apiKey === 'xxx') {
const [translation] = await translate.translate(text, target)
res.send({
code: 200,
data: {
text,
translation,
},
message: '成功',
})
} else {
res.send({
code: 400,
message: '失败:参数apiKey',
})
}
}
https://console.cloud.google.com/apis/dashboard?hl=zh-cn&project=xutongbao
参考链接: