尝试使用带有typscript的谷歌电子表格库时出现ERR_OSSL_UNSUPPORTED

问题描述 投票:0回答:4

我尝试执行此代码(从文档复制https://theoephraim.github.io/node-google-spreadsheet/#/):

async function accessSpreadsheet(){
    const doc = new GoogleSpreadsheet(process.env.spreadsheetId!);
    await doc.useServiceAccountAuth({
        // env var values are copied from service account credentials generated by google
        // see "Authentication" section in docs for more info
        client_email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL!,
        private_key: process.env.GOOGLE_PRIVATE_KEY!, 
    });
    await doc.loadInfo(); // loads document properties and worksheets
    console.log(doc.title);

}

accessSpreadsheet();

&我收到此错误:

library: 'DECODER routines',
reason: 'unsupported', 
code: 'ERR_OSSL_UNSUPPORTED

我不明白我是如何得到这个结果的。我尝试用谷歌搜索这个问题,尝试下载多个不同的依赖项并尝试不同的操作系统,但问题仍然存在。 谢谢你。

node.js typescript spreadsheet
4个回答
10
投票

我找到了解决方案。我的谷歌私钥应该是顺便说一句“”。


1
投票

对于任何需要创建服务帐户的人来说:

然后在内部创建 json 密钥,这将下载密钥并通过以下配置使用该密钥:

process.env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, "\n")


0
投票

我只是在这里插话一下我与 Circle CI 相关的经验。这个错误似乎意味着谷歌私钥有问题,正如其他人指出的那样。

我遇到的问题是 Circle CI 不能很好地处理多行环境变量。我最终遵循了这个:https://medium.com/@nemiga/multiline-env-secrets-in-circle-ci-ac234c075911

我对密钥进行了 Base64 编码,并将其放入 CircleCI 中,以便它将所有内容作为一行处理,然后再对其进行解码。

这似乎可以通过 Circle CI 为我解决这个问题。


0
投票

这将解决问题:

在读取环境变量时这样做。

const credential = JSON.parse(
  (process.env.GOOGLE_PRIVATE_KEY as string).toString().replace(/\n/g,"")
);

key: credential.private_key,

私钥必须是

GOOGLE_PRIVATE_KEY='{"---"}' // in .env.local

但是,当将密钥放入某些托管平台(在我的例子中是 vercel)时,它会成功

GOOGLE_PRIVATE_KEY={"---"} // in vercel environment variable value
© www.soinside.com 2019 - 2024. All rights reserved.