0

我是 JavaScript 新手。我正在尝试为服务器到服务器应用程序实现 OAuth 2.0,因为我正在使用这个库。所以当我这样做的时候

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 文件位于同一目录中。

4

1 回答 1

1

没有必要fs.readFileSync

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

只需提供简单的文件路径

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

原始文档中所述:

// 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',
于 2018-01-16T07:09:05.027 回答