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

 https://stackoverflow.com/questions/55732639/how-set-api-key-in-google-translate-node-js-code/68293290#68293290

参考链接:

https://chat.xutongbao.top/

 

posted @ 2023-09-30 11:55  徐同保  阅读(9)  评论(0编辑  收藏  举报  来源