【问题标题】:firebase MISSING_CUSTOM_TOKEN error while curl "https://identitytoolkit.googleapis.com/v1curl 时出现 firebase MISSING_CUSTOM_TOKEN 错误“https://identitytoolkit.googleapis.com/v1
【发布时间】:2020-04-21 00:08:16
【问题描述】:

目标:我想了解如何在 FireStore 中使用自定义令牌。

暂定:我尝试关注firebase curl example by

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/Web Api Key 复制的“Firebase Web API Key”。然后我添加了一个仅用于测试“123456”的令牌。如果我理解正确的文档,我应该得到一个令牌。

我的最终目标是为我的 Firebase 项目添加一个自定义令牌,它允许 Angular 客户端连接到 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);
  });

angular-firebase-auth0-3c084-firebase-adminsdk-lu97a-6ba2ba41e0.json 已从 Firebase 下载并包含:

{
  "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 的自定义令牌时我缺少什么?既然是Custom Token,至少对于Hello World,我可以用一个很简单的Token,比如“123456”吧?

第二个问题,在上面的 server.js 中,我是否提供了 admin.credential.cert 中预期的正确文件?

【问题讨论】:

    标签: node.js firebase firebase-authentication


    【解决方案1】:

    根据我对 Firebase 自定义令牌身份验证的理解,您不能像这样发送普通的未加密令牌。为了使其工作,您必须使用您已有的相同代码基于 uid 生成令牌,这可能是 123456

    const customToken = await admin.auth().createCustomToken("123456")
    

    然后使用那个customToken来发出curl命令的测试请求。

    对于您的第二个问题,admin.credential.cert() 命令上的服务帐户注入是正确的,如果您将实际的.json 文件与官方提供的示例Cloud IAM Documentation 进行比较,一切似乎都是正确的。

    唯一可能导致问题的是您的private_keyprivate_key_id 或任何其他需要替换的字段是否错误,但只有您可以检查。

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2014-01-03
      • 2014-12-24
      • 2020-09-15
      • 2019-04-16
      • 2018-12-26
      • 1970-01-01
      • 2021-01-21
      • 1970-01-01
      • 2021-08-15
      相关资源
      最近更新 更多