next.js Firebase管理员getUser无效授权

qni6mghb  于 2022-12-18  发布在  其他
关注(0)|答案(1)|浏览(134)

验证id令牌没有问题,但奇怪的是,当我使用firebaseAdmin.auth().getUser(uid)时,我得到以下错误:

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'
}

下面是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的存在只是为了说明问题。我怀疑这是一个权限问题。正在使用的服务帐户具有以下权限:

  • Firebase管理SDK管理员服务代理
  • Firebase应用程序检查管理员
  • Firebase实时数据库管理员
  • 服务帐户令牌创建者
  • 存储管理员
7nbnzgx9

7nbnzgx91#

我生成了一个新的管理SDK密钥(即使我已经有一个),这解决了这个问题。

相关问题