Home > front end >  Firebase admin getUser invalid grant
Firebase admin getUser invalid grant

Time:12-17

Verifying id token works without a problem, but strangely enough when I use firebaseAdmin.auth().getUser(uid) I get the following error:

FirebaseAppError: 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: invalid_grant (Invalid grant: account not found)". There are two likely causes: (1) your server time is not properly synced or (2) your certificate key file has been revoked. To solve (1), re-sync the time on your server. To solve (2), make sure the key ID for your key file is still present at https://console.firebase.google.com/iam-admin/serviceaccounts/project. If not, generate a new key file at https://console.firebase.google.com/project/_/settings/serviceaccounts/adminsdk.
    at C:\Users\dinop\Documents\VS Code\Personal\next_social_app\node_modules\firebase-admin\lib\app\firebase-app.js:85:19
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async getServerSideProps (webpack-internal:///./pages/user/[uid].tsx:30:30)
    at async Object.renderToHTML (C:\Users\dinop\Documents\VS Code\Personal\next_social_app\node_modules\next\dist\server\render.js:506:20)
    at async doRender (C:\Users\dinop\Documents\VS Code\Personal\next_social_app\node_modules\next\dist\server\base-server.js:708:34)
    at async cacheEntry.responseCache.get.isManualRevalidate.isManualRevalidate (C:\Users\dinop\Documents\VS Code\Personal\next_social_app\node_modules\next\dist\server\base-server.js:813:28)
    at async C:\Users\dinop\Documents\VS Code\Personal\next_social_app\node_modules\next\dist\server\response-cache\index.js:80:36 {
  errorInfo: {
    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: invalid_grant (Invalid grant: account not found)". There are two likely causes: (1) your server time is not properly synced or (2) your certificate key file has been revoked. To solve (1), re-sync the time on your server. To solve (2), make sure the key ID for your key file is still present at https://console.firebase.google.com/iam-admin/serviceaccounts/project. If not, generate a new key file at https://console.firebase.google.com/project/_/settings/serviceaccounts/adminsdk.'
  },
  codePrefix: 'app'
}

Just to clarify, this is a simple code snippet used in getServerSideProps:

const token = await firebaseAdmin.auth().verifyIdToken(cookies.token);
const pageUid: any = ctx.params?.uid;
console.log(pageUid); // gets printed, verifying id token works as explained.
const pageUserData = await firebaseAdmin.auth().getUser(pageUid); // throws the error

PageUid in this case exists just to be clear. I suspect it's a permission issue. The service account in use has the following permissions:

  • Firebase Admin SDK Administrator Service Agent
  • Firebase App Check Admin
  • Firebase Realtime Database Admin
  • Service Account Token Creator
  • Storage Admin

CodePudding user response:

I generated a new Admin SDK key (even though I already had one), this solved the issue.

  • Related