question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Firebase Cloud Functions: app/invalid-credential failed to fetch a valid Google OAuth2 access token Firebase Admin SDK

See original GitHub issue

[REQUIRED] Environment info

firebase-tools: 7.5.0

Platform: macOS

[REQUIRED] Test case

[REQUIRED] Steps to reproduce

I am new to Cloud Functions. I followed the docs, and the new initialization syntax for firebase-admin, but I am getting an auth error when running the function on the server and on a localhost.

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\"."}

I tried instantiating the sdk with a serviceAccountKey, same issue. https://firebase.google.com/docs/admin/setup#initialize_the_sdk

The function is being triggered on localhost and on the server, and error is being thrown once the call to the database is being made at gettingUser()

When running locally after building:

$ 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 articleId = context.params.articleId

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


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

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

  console.log(`${name} was added by: ${addedById}`);
  console.log(`burgersVal: ${articleVal}`);
  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()
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:9
  • Comments:39 (11 by maintainers)

github_iconTop GitHub Comments

39reactions
samtsterncommented, Jun 30, 2021

To anyone experiencing this issue:

  • If you’re seeing it in the emulator you can fix it by either setting GOOGLE_APPLICATION_CREDENTIALS to a service account or running gcloud auth application-default login on your machine.
  • If you’re seeing this in production then it may be that someone has modified the service accounts attached to your project that a Functions service experiencing a temporary hiccup. If the problem persists, please reach out to support.
21reactions
dooleyb1commented, Nov 18, 2019

After upgrading to Mac OS Catalina I encountered this issue. What resolved it for me was running the following commands:

  • firebase login
  • export GOOGLE_APPLICATION_CREDENTIALS="<PATH_TO_YOUR_SERVICE_ACCOUNT_KEY_JSON>"
Read more comments on GitHub >

github_iconTop Results From Across the Web

Error fetching access token Firebase Cloud Functions
via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "Error fetching access token: Error ......
Read more >
Learn about using and managing API keys for Firebase - Google
How do I fix this error? "Failed to fetch this Firebase app's measurement ID from the server." The API key used by your...
Read more >
腾讯云开发者社区-腾讯云
javascriptfirebasefirebase-cloud-messaginggoogle-cloud-functionsfirebase-admin ... "credential" property failed to fetch a valid Google OAuth2 access token ...
Read more >
getting a Google OAuth2 access token error ENOTFOUND
The Cloud Functions for Firebase SDK const functions ... "credential" property failed to fetch a valid Google OAuth2 access token with the ...
Read more >
jaybedeau/Firebase-Beginner - Gitter
... "credential" property failed to fetch a valid Google OAuth2 access token with ... I am using angular 5 How can we get...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found