'How do I convert the google private key for the google calendar api into a env file?
I'm trying to use JWT to access the google calendar api. I'm using Node to make my call. How can I get this line of JSON into my .env file:
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvAIBADAVznWpHSDMU9JXzBwdm1w4ILex6G8tEdEXa3\nr1YPynJ9aT9tPgrDXXnwMMrRd8q2Kvkqi6WaYMkCgYAvSsRQWuOGEkxVRkKG2VHYNW+XzMaiiHmUznfll/5\nsmYDwtRzhqN9eAsEjfj2K99lcJnd8fCKib5a5Ga+kgSBb64jrj9UOJUbOZJDVh4/\nckglGM61BAR+gdl3tCXY4w==\n-----END PRIVATE KEY-----\n"
(I shortened the key in this post to keep it safe and to keep this post clean).
I'm following this guide on setting up jwt to access the google calendar api: https://dev.to/megazear7/google-calendar-api-integration-made-easy-2a68
I can get it to work fine if I have the key as a string in the index.js file like this:
const GOOGLE_PRIVATE_KEY = "-----BEGIN PRIVATE KEY-----\nMIIEvAIBADAVznWpHSDMU9JXzBwdm1w4ILex6G8tEdEXa3\nr1YPynJ9aT9tPgrDXXnwMMrRd8q2Kvkqi6WaYMkCgYAvSsRQWuOGEkxVRkKG2VHYNW+XzMaiiHmUznfll/5\nsmYDwtRzhqN9eAsEjfj2K99lcJnd8fCKib5a5Ga+kgSBb64jrj9UOJUbOZJDVh4/\nckglGM61BAR+gdl3tCXY4w==\n-----END PRIVATE KEY-----\n"
But when I try to set this key in my .env file, like this:
GOOGLE_PRIVATE_KEY = -----BEGIN PRIVATE KEY-----\nMIIEvAIBADAVznWpHSDMU9JXzBwdm1w4ILex6G8tEdEXa3\nr1YPynJ9aT9tPgrDXXnwMMrRd8q2Kvkqi6WaYMkCgYAvSsRQWuOGEkxVRkKG2VHYNW+XzMaiiHmUznfll/5\nsmYDwtRzhqN9eAsEjfj2K99lcJnd8fCKib5a5Ga+kgSBb64jrj9UOJUbOZJDVh4/\nckglGM61BAR+gdl3tCXY4w==\n-----END PRIVATE KEY-----\n
and then access it in my index.js file like this:
const GOOGLE_PRIVATE_KEY = process.env.GOOGLE_PRIVATE_KEY
I get this error:
{"errors":{"library":"PEM routines","function":"get_name","reason":"no start line","code":"ERR_OSSL_PEM_NO_START_LINE"}}
Any thoughts? :)
Solution 1:[1]
When you put your key on .env, extra slashes are added on the string.
process.env.GOOGLE_PRIVATE_KEY.replace(new RegExp("\\\\n", "\g"), "\n")
This will fix it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | robert-the-freaking-victor |