我是 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 文件位于同一目录中。
最佳答案
不需要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',
关于javascript - 如何获取js文件中的pem文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48275624/