Search code examples
javascriptnode.jspemserver-to-server

How to get pem file in js file


I am new to javascript. I am trying to implement OAuth 2.0 for Server to Server Applications for that i am using this library. So while i was doing 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);
});

it gave me following exception

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

my file pem.key file is in the same directory in which my js file is.


Solution

  • There is no need of fs.readFileSync

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

    Just give simple path to file

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

    As given in 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',