作者:大道废_796 | 来源:互联网 | 2022-12-06 17:38
如何解决《使用JWT和googleapis进行身份验证时出错(错误:0906D06C:PEM例程:PEM_read_bio:无起始行)》经验,为你挑选了1个好方法。
我正在尝试从我的节点服务器(作为Firebase云功能运行)连接到Google Analytics Reporting API v4.我想使用JWT进行身份验证,例如:http://nali.org/google-analytics-v4-api-node-js-starter-example/
但是我收到此错误:
Error: error:0906D06C:PEM routines:PEM_read_bio:no start line
at Error (native)
at Sign.sign (crypto.js:307:26)
at Object.sign (/user_code/node_modules/googleapis/node_modules/jwa/index.js:76:45)
at Object.jwsSign [as sign] (/user_code/node_modules/googleapis/node_modules/jws/lib/sign-stream.js:32:24)
它似乎试图使用PEM文件,但我只想使用电子邮件和私钥.这不可能吗?我给出的示例链接是误导性的吗?
这是我的代码:
const email = functions.config().ga.email;
const privateKey = functions.config().ga.privatekey;
const scope = ['https://www.googleapis.com/auth/analytics.readonly'];
const token = new googleapis.google.auth.JWT(email, null, privateKey, scope, null);
token.authorize( (error, tokens) => {
if (error) {
console.error('Error connecting to GA:', error);
} else {
console.log('Authorized!', tokens);
}
})
谢谢你的帮助!
1> ian..:
新秀错误:我没有\n
在我的privateKey字符串中转义。firebase functions:config:set
转换\n
为\\n
固定代码看起来像:
const email = functions.config().ga.email;
const privateKey = _.replace(functions.config().ga.privatekey, /\\n/g, '\n');
const scope = ['https://www.googleapis.com/auth/analytics.readonly'];
const jsOnWebToken= new googleapis.google.auth.JWT(email, null, privateKey, scope, null);
jsonWebToken.authorize( (error, tokens) => {
if (error) {
console.error('Error connecting to GA:', error);
} else {
console.log('Authorized!', tokens);
}
});
我的实际问题已使用以下建议解决:使用Firebase privateKey作为Heroku配置变量转义问题