【问题标题】:Google translate key missing on node js ajax节点js ajax上缺少谷歌翻译键
【发布时间】:2019-12-04 17:05:05
【问题描述】:

我有一个简单的 Node JS 脚本,在终端本地运行时效果很好:

exports.google_translate    = function (translate_text, res) {
var Translate       = require('@google-cloud/translate');
var translate       = new Translate.Translate({projectId: 'my project'});

translate.translate(translate_text, 'fr').then(results => {
    var translation     = results[0];
    res.send(translation);
}).catch(err => {
    res.send('ERROR:', err);
});
}

但是,每当我通过 Ajax 调用它时,我都会收到以下错误:

Error: The request is missing a valid API key.

我已经使用这个将它添加为永久环境变量:

export GOOGLE_APPLICATION_CREDENTIALS="[PATH to key downloaded]"

但是每次我通过 Ajax 调用这个脚本时,我都会得到同样的错误。所以我的问题是,如何让 Node JS 脚本保存 API 密钥,以便在通过 Ajax 调用时工作?

谢谢

【问题讨论】:

  • 你试过将json文件的路径传递给构造函数吗? Translate({ keyFilename: '/path/to/keyfile.json'})

标签: javascript node.js ajax google-translate


【解决方案1】:
const {Translate} = require('@google-cloud/translate').v2;    
const translate = new Translate({
        credentials: {
        "type": "account",
        "project_id": "your_project",
        "private_key_id": "your_data",
        "private_key": "your_data",
        "client_email": "your_data",
        "client_id": "your_data",
        "auth_uri": "your_data",
        "token_uri": "your_data",
        "auth_provider_x509_cert_url": "your_data",
        "client_x509_cert_url": "your_data"
        }
      });
const text = 'This is testing!';
const target = 'de';

async function translateText() {
    // Translates the text into the target language. "text" can be a string for
    // translating a single piece of text, or an array of strings for translating
    // multiple texts.
    let [translations] = await translate.translate(text, target);
    translations = Array.isArray(translations) ? translations : [translations];
    console.log('Translations:' + translations);
  }
  
translateText();

您必须从您在 Google Cloud 上的项目中获取此 credentials.json 文件。他们会给你一个文件.json

【讨论】:

    【解决方案2】:

    似乎无论出于何种原因,应用程序都无法正确读取环境变量。由于 nodejs 将所有环境变量存储在 process.env 中,您可以通过调用来确保它是编写的:

    function google_translate(translate_text) {
        process.env.GOOGLE_APPLICATION_CREDENTIALS = "[PATH to key downloaded]";
    
        return translate.translate(translate_text, 'fr')
            .then(console.log)
            .catch(console.error);
    }
    

    或将密钥直接传递给构造函数

    const translate = new Translate.Translate({
        projectId: 'my-project',
        keyFilename: "[PATH to key downloaded]"
    });
    

    您还可以确保最终读取密钥文件并将配置传递给翻译构造函数

    const translate = new Translate.Translate({
        credentials: JSON.parse(fs.readFileSync("[PATH to key downloaded]", "utf8"))
    });
    

    如果仍然没有帮助,可能是密钥本身的问题,您可以尝试在此处生成一个新的 https://console.cloud.google.com/apis/credentials

    【讨论】:

    • 我使用您的解决方案使其工作,但发现 new Translate.Translate() 不是构造函数,但 new Translate() 工作
    • 这取决于你如何导入函数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    • 2016-12-28
    • 2019-05-20
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多