【问题标题】:How to get pem file in js file如何在js文件中获取pem文件
【发布时间】:2018-06-24 19:54:50
【问题描述】:

我是 JavaScript 新手。我正在尝试实现OAuth 2.0 for Server to Server Applications,因为我正在使用this 库。所以当我这样做的时候

googleAuth.authenticate(
{
email: 'my.gserviceaccount.com',
keyFile: fs.readFileSync("./accesstoken/key.pem"),
scopes: ['https://www.googleapis.com/auth/drive.readonly']
}, 
function (err, token) {
  console.log(token);
  console.log("err:"+err);
});

它给了我以下异常

ENOENT: no such file or directory, open '-----BEGIN PRIVATE KEY-----asdasxxx---END PRIVATE KEY-----

我的文件 pem.key 文件在我的 js 文件所在的同一目录中。

【问题讨论】:

    标签: javascript node.js pem server-to-server


    【解决方案1】:

    不需要fs.readFileSync

    keyFile: fs.readFileSync("./accesstoken/key.pem"),
    

    只需给出简单的文件路径

    keyFile: "./key.pem", // if file is in same folder
    

    Original Doc 中所述:

    // the path to the PEM file to use for the cryptographic key (ignored if 'key' is also defined) 
    // the key will be used to sign the JWT and validated by Google OAuth 
    keyFile: 'path/to/key.pem',
    

    【讨论】:

    • @FaisalAhmed,很高兴知道,快乐编码:)
    猜你喜欢
    • 2021-12-30
    • 2010-11-02
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    • 1970-01-01
    • 2021-08-22
    相关资源
    最近更新 更多