如何在js文件中获取pem文件

How to get pem file in js file

我是 javascript 的新手。我正在尝试实施 OAuth 2.0 for Server to Server Applications for that i am using 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 文件在同一目录中。

不需要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',