试探性的:我试着跟着
firebase curl example
curl "https://identitytoolkit.googleapis.com/v1/accounts:signInWithCustomToken?key=AIzaSyCO0B7UXo2OcfhXQ2gBxHAPuN5muusiIFw" -H 'Content-Type: application/json' --data-binary '{"token":"123456","returnSecureToken":true}'
我得到了
{
"error": {
"code": 400,
"message": "MISSING_CUSTOM_TOKEN",
"errors": [
{
"message": "MISSING_CUSTOM_TOKEN",
"domain": "global",
"reason": "invalid"
}
]
}
}
curl: (6) Could not resolve host: application
上面的curl命令是用我从Firebase/my Project/General/webapi密匙复制的“firebasebapi密钥”填充的。然后我添加了一个令牌来测试“123456”。如果我正确理解文档,我应该拿回一个令牌。
我的最终目标是向我的Firebase项目添加一个自定义令牌,允许角度客户端连接到Firestore,并在文档更改时得到通知。
对于创建Custon令牌,我发现的最佳步骤是
Stackoverflow Custom Token Question
. 所以我试着用:
const admin = require('firebase-admin');
const serviceAccount = require('./angular-firebase-auth0-3c084-firebase-adminsdk-lu97a-6ba2ba41e0.json')
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
var uid = "some-uid";
var claim = {
control: true
};
admin.auth().createCustomToken(uid, true)
.then(function (customToken) {
console.log(customToken)
})
.catch(function (error) {
console.log("Error creating custom token:", error);
});
firebase-086bASE-6bASE-086bASE-6bASE-6bASE-6bASE-6bASE-6bASE-6bASE-6bASE-DOWnA包含的fire0d0
{
"type": "service_account",
"project_id": "angular-firebase-auth0-3c084",
"private_key_id": "6ba2ba41e0bf3837841aa9772c7d880b7ce3be81",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAg ...... +n+uYQTJSJPM7Tvgfssa8X1KK09zoj2f7ZLvcjGzl/VF2D7uf23VtAL2RZsB7z14\ny4rnDCc4Rx7nslGUk6kwEz+xJYUpP96rkp5iv/qUuoveJdI/NogJjgUtvRUa2evA\ntg2PV9xsYvkt8+8Ce79fYKA=\n-----END PRIVATE KEY-----\n",
"client_email": "firebase-adminsdk-lu97a@angular-firebase-auth0-3c084.iam.gserviceaccount.com",
"client_id": "114324662014690107039",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-lu97a%40angular-firebase-auth0-3c084.iam.gserviceaccount.com"
}
我得到了
C:\WSs\FireStoreDemos\firestore-custom-token>node server
C:\WSs\FireStoreDemos\firestore-custom-token\node_modules\firebase-admin\lib\auth\token-generator.js:205
throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, errorMessage);
^
FirebaseAuthError: `developerClaims` argument must be a valid, non-null object containing the developer claims.
at FirebaseAuthError.FirebaseError [as constructor] (C:\WSs\FireStoreDemos\firestore-custom-token\node_modules\[4mfirebase-admin[24m\lib\utils\error.js:42:28)
at FirebaseAuthError.PrefixedFirebaseError [as constructor] (C:\WSs\FireStoreDemos\firestore-custom-token\node_modules\[4mfirebase-admin[24m\lib\utils\error.js:88:28)
at new FirebaseAuthError (C:\WSs\FireStoreDemos\firestore-custom-token\node_modules\[4mfirebase-admin[24m\lib\utils\error.js:147:16)
at FirebaseTokenGenerator.createCustomToken (C:\WSs\FireStoreDemos\firestore-custom-token\node_modules\[4mfirebase-admin[24m\lib\auth\token-generator.js:205:19)
at Auth.BaseAuth.createCustomToken (C:\WSs\FireStoreDemos\firestore-custom-token\node_modules\[4mfirebase-admin[24m\lib\auth\auth.js:94:36)
at Object.<anonymous> (C:\WSs\FireStoreDemos\firestore-custom-token\server.js:12:14)
[90m at Module._compile (internal/modules/cjs/loader.js:1147:30)[39m
[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)[39m
[90m at Module.load (internal/modules/cjs/loader.js:996:32)[39m
[90m at Function.Module._load (internal/modules/cjs/loader.js:896:14)[39m {
errorInfo: {
code: [32m'auth/argument-error'[39m,
message: [32m'`developerClaims` argument must be a valid, non-null object containing the developer claims.'[39m
},
codePrefix: [32m'auth'[39m
}
所以,我的主要问题是:我缺少什么来创建一个上面有curl的自定义令牌?既然是自定义令牌,至少对于Hello World来说,我可以使用一个非常简单的令牌,比如“123456”,对吗?
第二个问题,在上面服务器.js,我是否提供了中所需的正确文件admin.credential.cert?