无法使用firebase-admin获取有效的Google OAuth2访问令牌

时间:2017-04-09 17:32:37

标签: node.js firebase oauth-2.0 firebase-admin

我目前无法使用nodejs中的firebase-admin读取或写入数据。

如果没有启用日志记录,它只会永久挂起,永远不会返回错误,也永远不会完成。

启用日志记录后,它会提供以下内容(它会继续按计划进行重试 - 但结果相同):

p:0: Browser went online.
0: set {"path":"/server/saving-data/fireblog/users","value":{"alanisawesome":{"date_of_birth":"June 23, 1912","full_name":"Alan Turing"},"gracehop":{"date_of_birth":"December 9, 1906","full_name":"Grace Hopper"}},"zg":null}
p:0: Buffering put: /server/saving-data/fireblog/users
p:0: Making a connection attempt
p:0: Failed to get token: Error: Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "unable to verify the first certificate".
p:0: data client disconnected
p:0: Trying to reconnect in 0ms
0: onDisconnectEvents

当我抓住(firebase-admin内部)https请求时,我得到以下内容:

{ Error: unable to verify the first certificate
    at TLSSocket.<anonymous> (_tls_wrap.js:1094:38)
    at emitNone (events.js:86:13)
    at TLSSocket.emit (events.js:188:7)
    at TLSSocket._finishInit (_tls_wrap.js:616:8)
    at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:446:38) code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE' }

我已尝试从控制台生成新的私钥,但它会继续返回相同的错误。我还在其他地方添加了require(&#39; ssl-root-cas&#39;)。inject(),但它并没有改变行为。将实际的json对象传递给admin.credential.cert函数也不会改变行为。

我目前在节点v7.7.4上。

以下是产生错误的代码:

const admin = require('firebase-admin');

admin.initializeApp({
  credential: admin.credential.cert('./serviceAccountKey.json'),
  databaseURL: 'https://mtg-decks-bc92d.firebaseio.com'
});

// turn on logging
admin.database.enableLogging(true);

const db = admin.database();
const ref = db.ref('server/saving-data/fireblog');

const usersRef = ref.child('users');
usersRef.set({
  alanisawesome: {
    date_of_birth: 'June 23, 1912',
    full_name: 'Alan Turing'
  },
  gracehop: {
    date_of_birth: 'December 9, 1906',
    full_name: 'Grace Hopper'
  }
}).then(() => {
  console.log('set complete');
});

2 个答案:

答案 0 :(得分:1)

其他一些帖子建议通过调用require('ssl-root-cas').inject();添加根证书。你能试试吗?

示例:Error: unable to verify the first certificate in nodejs

答案 1 :(得分:0)

我收到此错误是因为我的本地计算机时区是手动设置的,因此令牌到期等已关闭。