Firebase admin().auth().getUser(uid) Error: An internal error has occurred 'auth/internal-error'
Firebase admin().auth().getUser(uid) Error: An internal error has occurred 'auth/internal-error'
我在节点服务器上使用 firebase-admin
初始化管理应用程序工作正常:
const admin = require('firebase-admin')
const serviceAccount = require('../service-account.json')
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: // firebaseio.com url
})
admin.auth().verifyIdToken(token).then((decoded) => {
// THIS WORKS SUCCESS! a decoded token
console.log('decoded', decoded)
// now look up the user information
admin.auth().getUser(decoded.uid).then((userRecord) => {
console.log('got user record', userRecord)
}).catch((err) => {
// { [Error: An internal error has occurred.]
// errorInfo:
// { code: 'auth/internal-error',
// message: 'An internal error has occurred.' } }
console.error(err)
})
})
getUser
的最后一部分因
而失败
{ [Error: An internal error has occurred.]
errorInfo:
{ code: 'auth/internal-error',
message: 'An internal error has occurred.' } }
当我尝试以其他推荐方式传递凭据时,我得到了这个:
const admin = require('firebase-admin')
admin.initializeApp({
credential: admin.credential.cert({
projectId: //projectid,
clientEmail: //client email,
privateKey: //admin private key
}),
databaseURL: // firebaseio.com url
})
然后当我尝试验证和查找用户时:
admin.auth().verifyIdToken(token).then((decoded) => {
// THIS WORKS SUCCESS! a decoded token
console.log('decoded', decoded)
// now look up the user information
admin.auth().getUser(decoded.uid).then((userRecord) => {
console.log('got user record', userRecord)
}).catch((err) => {
// Error: error:0906D06C:PEM routines:PEM_read_bio:no start line
console.error(err)
})
})
getUser
的最后一部分因
而失败
Error: error:0906D06C:PEM routines:PEM_read_bio:no start line
这里的问题是正在使用的服务帐户没有明确下载用户的项目权限。
事实证明,解码令牌只需要服务帐户,但其他操作需要额外的权限,例如 getUser(uid)
为了解决这个问题,我必须进入项目权限中的 IAM 部分,并为具有适当权限的服务器帐户添加电子邮件地址(我做了编辑)。
步骤
- 设置图标 -> 权限
- IAM
- + 添加用户图标
- 输入服务帐户凭据的电子邮件地址并分配角色
生成有效服务帐户密钥文件的最简单方法是按照 Add Firebase to your app 中的说明进行操作。有一个很好的 UI,您可以使用它来生成一个新的 JSON 文件,该文件应该具有您的项目的所有适当权限。然后您可以使用它来初始化 Admin Node.js SDK,如下所示:
var serviceAccount = require("path/to/serviceAccountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
我在节点服务器上使用 firebase-admin
初始化管理应用程序工作正常:
const admin = require('firebase-admin')
const serviceAccount = require('../service-account.json')
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: // firebaseio.com url
})
admin.auth().verifyIdToken(token).then((decoded) => {
// THIS WORKS SUCCESS! a decoded token
console.log('decoded', decoded)
// now look up the user information
admin.auth().getUser(decoded.uid).then((userRecord) => {
console.log('got user record', userRecord)
}).catch((err) => {
// { [Error: An internal error has occurred.]
// errorInfo:
// { code: 'auth/internal-error',
// message: 'An internal error has occurred.' } }
console.error(err)
})
})
getUser
的最后一部分因
{ [Error: An internal error has occurred.] errorInfo: { code: 'auth/internal-error', message: 'An internal error has occurred.' } }
当我尝试以其他推荐方式传递凭据时,我得到了这个:
const admin = require('firebase-admin')
admin.initializeApp({
credential: admin.credential.cert({
projectId: //projectid,
clientEmail: //client email,
privateKey: //admin private key
}),
databaseURL: // firebaseio.com url
})
然后当我尝试验证和查找用户时:
admin.auth().verifyIdToken(token).then((decoded) => {
// THIS WORKS SUCCESS! a decoded token
console.log('decoded', decoded)
// now look up the user information
admin.auth().getUser(decoded.uid).then((userRecord) => {
console.log('got user record', userRecord)
}).catch((err) => {
// Error: error:0906D06C:PEM routines:PEM_read_bio:no start line
console.error(err)
})
})
getUser
的最后一部分因
Error: error:0906D06C:PEM routines:PEM_read_bio:no start line
这里的问题是正在使用的服务帐户没有明确下载用户的项目权限。
事实证明,解码令牌只需要服务帐户,但其他操作需要额外的权限,例如 getUser(uid)
为了解决这个问题,我必须进入项目权限中的 IAM 部分,并为具有适当权限的服务器帐户添加电子邮件地址(我做了编辑)。
步骤
- 设置图标 -> 权限
- IAM
- + 添加用户图标
- 输入服务帐户凭据的电子邮件地址并分配角色
生成有效服务帐户密钥文件的最简单方法是按照 Add Firebase to your app 中的说明进行操作。有一个很好的 UI,您可以使用它来生成一个新的 JSON 文件,该文件应该具有您的项目的所有适当权限。然后您可以使用它来初始化 Admin Node.js SDK,如下所示:
var serviceAccount = require("path/to/serviceAccountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});