热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用JWT和googleapis进行身份验证时出错(错误:0906D06C:PEM例程:PEM_read_bio:无起始行)

如何解决《使用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配置变量转义问题


推荐阅读
author-avatar
大道废_796
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有