【问题标题】:Error authenticating with JWT and googleapis (error:0906D06C:PEM routines:PEM_read_bio:no start line)使用 JWT 和 googleapis 进行身份验证时出错(错误:0906D06C:PEM 例程:PEM_read_bio:no start line)
【发布时间】:2019-01-31 07:21:39
【问题描述】:

我正在尝试从我的节点服务器(作为 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);
  }
})

感谢您的帮助!

【问题讨论】:

    标签: node.js google-api google-analytics-firebase google-api-nodejs-client


    【解决方案1】:

    新手错误:我没有转义 privateKey 字符串中的\nfirebase 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);
      }
    });
    

    我的实际问题已通过以下建议解决:Escaping issue with firebase privateKey as a Heroku config variable

    【讨论】:

      猜你喜欢
      • 2017-02-18
      • 2020-07-16
      • 2018-07-04
      • 2018-09-01
      • 2018-11-01
      • 2020-11-07
      • 2018-06-03
      • 2023-02-24
      • 1970-01-01
      相关资源
      最近更新 更多