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',
    })
  }
}

 node使用@google-cloud/translate调用google翻译api,How set API KEY in Google Translate Node.js code_第1张图片

https://console.cloud.google.com/apis/dashboard?hl=zh-cn&project=xutongbao

node使用@google-cloud/translate调用google翻译api,How set API KEY in Google Translate Node.js code_第2张图片

node使用@google-cloud/translate调用google翻译api,How set API KEY in Google Translate Node.js code_第3张图片

node使用@google-cloud/translate调用google翻译api,How set API KEY in Google Translate Node.js code_第4张图片

node使用@google-cloud/translate调用google翻译api,How set API KEY in Google Translate Node.js code_第5张图片

node使用@google-cloud/translate调用google翻译api,How set API KEY in Google Translate Node.js code_第6张图片

node使用@google-cloud/translate调用google翻译api,How set API KEY in Google Translate Node.js code_第7张图片

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

node使用@google-cloud/translate调用google翻译api,How set API KEY in Google Translate Node.js code_第8张图片

参考链接:

https://chat.xutongbao.top/

 

你可能感兴趣的:(web前端,前端,javascript,linux)