Firebase云功能:app / invalid-credential无法获取有效的Google OAuth2访问令牌Firebase Admin SDK

时间:2019-10-09 20:14:41

标签: typescript firebase firebase-realtime-database google-cloud-functions firebase-admin

我是Cloud Functions的新手。我遵循了文档以及firebase-admin的新初始化语法,但是在服务器和本地主机上运行该函数时遇到了auth错误。

https://firebase.google.com/docs/functions/beta-v1-diff#new_initialization_syntax_for_firebase-admin

@firebase/database: FIREBASE WARNING: {"code":"app/invalid-credential","message":"Credential implementation provided to initializeApp() via the \"credential\" property failed to fetch a valid Google OAuth2 access token with the following error: \"Error fetching access token: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal. Error code: ENOTFOUND\"."}

我尝试用serviceAccountKey实例化sdk,同样的问题。 https://firebase.google.com/docs/admin/setup#initialize_the_sdk

该函数在localhost和服务器上被触发,并且一旦在gettingUser()上对数据库的调用就会引发错误

在构建后在本地运行时:

$ firebase experimental:functions:shell
firebase > const data = {some_article...}
firebase > addingNewArticle(data)
'Successfully invoked function.'
firebase > >  New article created articleId1

DEBUG: @firebase/database: FIREBASE WARNING: {"code":"app/invalid-credential","message":"Credential implementation provided to initializeApp() via the \"credential\" property failed to fetch a valid Google OAuth2 access token with the following error: \"Error fetching access token: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal. Error code: ENOTFOUND\"."}

/index.ts

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

admin.initializeApp();

export const addingNewArticle = functions.database
.ref('/articles/{articleId}')
.onCreate(async (snapshot, context) => {
  const burgerId = context.params.burgerId

  const result = await gettingUser();
  console.log(`From onCreate ${result}`);


  console.log(`New article created ${articleId}`)

  const burgersVal = snapshot.val()
  const name = burgersVal.name
  const addedById = burgersVal.addedById

  console.log(`${name} was added by: ${addedById}`);
  console.log(`burgersVal: ${burgersVal}`);
  console.log(`snapshot: ${snapshot}`);


  return Promise.all([])
})


export async function gettingUser() {
    const result = await admin.database().ref(`users/fnkjasdfadsfna.d`).once('value');
    console.log(`Looging new user ${result.val()}`);
    return result.val()
}

0 个答案:

没有答案