google-api-nodejs-client: UnhandledPromiseRejectionWarning: Error: error:0906D06C:PEM routines:PEM_read_bio:no start line

I get the above error when I try initializing the drive with the jwt token. Below’s my implementation. the keys are valid and I downloaded the file from the google developer console.

...
const jwtClient = new google.auth.JWT({                                          
  email: secret.client_email,                                                    
  key: secret.private_key,                                                       
  scopes                                                                         
})                                                                               

const drive = google.drive({                                                     
  version: 'v3',                                                                 
  auth: jwtClient                                                                
})   

function workspace () {                                                          
  return new Promise(async (resolve, reject) => {                                
    const folder = await findFolder(workDir)                                     
    if (folder) return resolve(folder)                                           
     return createFolder(workDir)                                                 
       .then(({ data }) => resolve(data))                                         
        .catch(err => reject(err))                                                 
   })                                                                             
} 
...

const cwd = await workspace()
console.log(cwd)

workDir is a variable holding a reference to a string with the name of a working directory.

The weird thing is that this was working last week but then it stopped working for some reason.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 21 (8 by maintainers)

Most upvoted comments

i was using .env as well and this problem happened to me. To fix, I just had to wrap the value in the .env file in double quotes. Hope it helps someone.

NODE_ENV=production
API_KEY=abc
GOOGLE_API_KEY="-----BEGIN PRIVATE ... asdfasdf\n-----END PRIVATE KEY-----\n"

(edit) the other thing that just happened when going to prod was that something between the ENV var setting and the app replaced all \n with \\n, so i just had to do:

googleApiKey = _.replace(process.env.GOOGLE_API_KEY, new RegExp("\\\\n", "\g"), "\n")

i was using .env as well and this problem happened to me. To fix, I just had to wrap the value in the .env file in double quotes. Hope it helps someone.

NODE_ENV=production
API_KEY=abc
GOOGLE_API_KEY="-----BEGIN PRIVATE ... asdfasdf\n-----END PRIVATE KEY-----\n"

(edit) the other thing that just happened when going to prod was that something between the ENV var setting and the app replaced all \n with \\n, so i just had to do:

googleApiKey = _.replace(process.env.GOOGLE_API_KEY, new RegExp("\\\\n", "\g"), "\n")

THANK YOU!

So I doubt this is the problem, but I am guessing you can simplify your workspace function like this 😄

async function workspace () {                                                          
  const folder = await findFolder(workDir);
  if (folder) {
    return folder;
  }
  return createFolder(workDir);
} 

Now onto the error. This often happens when you accidentally provide the wrong kind of key. Please be 100% sure that secret.private_key contains the data you’d expect to see by doing a console.log(secret) at the top. My guess is that either a.) it’s returning empty for some reason or b.) you’re accidentally passing a public key.

Please try this stuff and let me know how it goes!

I know this is a silly question… the actual private_key_id you get back isn’t blank … is it? If you’re doing something with dotenv, I’d suggest you’re super sure what’s getting passed into your objects 😃

At runtime, add a console.log(secrets) right before you load em up. Feel free to twiddle the values a bit as not to expose your keys, but do that and paste the result here 😃

Using Azure DevOps pipelines with App Settings variable injections, you will run in to the same issue with API keys. They add an extra ** to all \n in the key. @bradwbradw hit the nail on the head with the solution.

My end result: const privateKey = process.env.GSUITE_PRIVATE_KEY.replace(new RegExp('\\\\n', '\g'), '\n')

It seems to be working though. It’s one of those strange errors that goes away once you restart your computer. I’ll go ahead and close this ticket. Thanks again @JustinBeckwith

haha @JustinBeckwith yeah, definitely not blank. There’s a weird behavior with dotenv and multi-line secrets. We all know this 😄. I’ll give it another try.

Happy it’s working 🎉