Firebase 实时数据库 - 如何使用 REST API 管理数据库规则

Cly*_*row 5 kotlin firebase firebase-realtime-database

我想使用 Rest API 获取/设置实时数据库规则,但是没有教程适合我。我尝试这样做:

  • 我将网址复制到位于欧洲的实时数据库https://my-project-database.europe-west1.firebasedatabase.app/
  • 我从本教程复制了java代码: https: //firebase.google.com/docs/database/rest/auth应该给我访问令牌
    val googleCred = GoogleCredential.fromStream(File("/path/to/my/key.json").inputStream())
    val scoped = googleCred.createScoped(
        Arrays.asList( // or use firebase.database.readonly for read-only access
            "https://www.googleapis.com/auth/firebase.database",
            "https://www.googleapis.com/auth/userinfo.email"
        )
    )
    scoped.refreshToken()
    val token = scoped.accessToken
    println(token)
Run Code Online (Sandbox Code Playgroud)

然而,令牌看起来很奇怪,末尾有一长串点(它与:为什么我从 Google OAuth 得到带有一堆句点/点的 JWT?

ya29.c.Kp8BCgi0lxWtUt-_[普通 JWT 内容,出于安全原因进行了编辑]yVvGk................................. ...................................................... ...................................................... ...................................................... ...................................................... ...................................................... ...................................................... ...................................................... ...................................................... ...................................................... ...................................................... ...................................................... ...................................................... ...................................................... ...................................................... ......................................

  • 我对类似地址进行了 HTTP GET 操作https://my-project-database.europe-west1.firebasedatabase.app/.settings/rules.json?access_token=$token,得到了 401 UNAUTHORIZED

我认为这是因为我使用了整个这个充满点的奇怪标记作为变量access_token。所以现在我有疑问如何改造它并使用它来access_token使其工作

编辑:我创建了这个要点,虽然它是在 python 中,但它有完全相同的问题。如何让它发挥作用? https://gist.github.com/solveretur/86d53a9c0221f096c38c3ef8f70a8dbd

Cly*_*row 0

它有效,我使用了错误的数据库密钥