NodeJS 错误:0909006 C:PEM例程:get_name:没有起始行-用于heroku中的Google云平台

9fkzdhlc  于 2022-12-22  发布在  Node.js
关注(0)|答案(2)|浏览(301)

当我尝试使用@google-cloud/storage时,它返回此错误

Error: error:0909006C:PEM routines:get_name:no start line 
2021-12-13T10:36:19.800822+00:00 app[web.1]: at Sign.sign (internal/crypto/sig.js:110:29)
2021-12-13T10:36:19.800823+00:00 app[web.1]: at Object.sign (/app/node_modules/gtoken/node_modules/jwa/index.js:152:45) 
2021-12-13T10:36:19.800823+00:00 app[web.1]: at Object.jwsSign [as sign] (/app/node_modules/gtoken/node_modules/jws/lib/sign-stream.js:32:24) 
2021-12-13T10:36:19.800824+00:00 app[web.1]: at GoogleToken.requestToken (/app/node_modules/gtoken/build/src/index.js:225:31) 
2021-12-13T10:36:19.800824+00:00 app[web.1]: at GoogleToken.getTokenAsyncInner (/app/node_modules/gtoken/build/src/index.js:163:21) 
2021-12-13T10:36:19.800824+00:00 app[web.1]: at GoogleToken.getTokenAsync (/app/node_modules/gtoken/build/src/index.js:142:55) 
2021-12-13T10:36:19.800825+00:00 app[web.1]: at GoogleToken.getToken (/app/node_modules/gtoken/build/src/index.js:94:21) 
2021-12-13T10:36:19.800825+00:00 app[web.1]: at JWT.refreshTokenNoCache (/app/node_modules/google-auth-library/build/src/auth/jwtclient.js:171:36) 
2021-12-13T10:36:19.800826+00:00 app[web.1]: at JWT.refreshToken (/app/node_modules/google-auth-library/build/src/auth/oauth2client.js:152:24) 
2021-12-13T10:36:19.800826+00:00 app[web.1]: at JWT.getRequestMetadataAsync (/app/node_modules/google-auth-library/build/src/auth/oauth2client.js:284:28) 
2021-12-13T10:36:19.800827+00:00 app[web.1]: at JWT.getRequestMetadataAsync (/app/node_modules/google-auth-library/build/src/auth/jwtclient.js:94:26) 
2021-12-13T10:36:19.800827+00:00 app[web.1]: at JWT.getRequestHeaders (/app/node_modules/google-auth-library/build/src/auth/oauth2client.js:247:37) 
2021-12-13T10:36:19.800827+00:00 app[web.1]: at GoogleAuth.authorizeRequest (/app/node_modules/google-auth-library/build/src/auth/googleauth.js:600:38) 
2021-12-13T10:36:19.800828+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:95:5) 
2021-12-13T10:36:19.800828+00:00 app[web.1]: at async Promise.all (index 1) 
{ 2021-12-13T10:36:19.800829+00:00 app[web.1]: library: 'PEM routines', 
2021-12-13T10:36:19.800829+00:00 app[web.1]: function: 'get_name', 
2021-12-13T10:36:19.800829+00:00 app[web.1]: reason: 'no start line', 
2021-12-13T10:36:19.800829+00:00 app[web.1]: code: 'ERR_OSSL_PEM_NO_START_LINE' 
2021-12-13T10:36:19.800830+00:00 app[web.1]: } 
2021-12-13T10:36:19.800879+00:00 app[web.1]: Error error:0909006C:PEM routines:get_name:no start line

该错误似乎是基于Google云服务帐户的私钥

-----BEGIN PRIVATE KEY-----
ENTIREKEY
-----END PRIVATE KEY-----
fcg9iug3

fcg9iug31#

我通过删除键中的\n行并手动添加一个新行来解决这个问题,例如:
私钥:\n---BEGIN PRIVATE KEY----\nShfkkJHNJK\nJeidj\n---END PRIVATE KEY----\n
收件人:

---BEGIN PRIVATE KEY----
ShfkkJHNJK
Jeidj
---END PRIVATE KEY----

注意:不要忘记最后一个\n

2j4z5cfb

2j4z5cfb2#

看起来问题出在"如何导入"密钥上,有几种解决方案,但在我的例子中,当我从env变量导入密钥时,我希望最好的解决方案是在我想从env导入密钥时添加.replace(/\\n/gm, "\n")
之前:

process.env.PRIVATE_KEY

之后:

process.env.PRIVATE_KEY?.replace(/\\n/gm, "\n")

相关问题