首页 文章

如何在firebase中验证休息呼叫?

提问于
浏览
16

我希望在我的用户的帮助下对某些受某些规则保护的数据进行休息调用,因此我需要将令牌发送给我的请求 . 根据firebase文档的哪个版本,有不同的方式:旧的和不推荐的方式(https://www.firebase.com/docs/rest/api/):

'https://samplechat.firebaseio-demo.com/users/jack/name.json?auth=<TOKEN>'

新的方式,我引用文档(https://firebase.google.com/docs/reference/rest/database/user-auth#section-get):

使用访问令牌Database REST API将接受查询字符串或标头上的access_token = Authenticate:Bearer以使用服务帐户验证请求 .

'https://samplechat.firebaseio-demo.com/users/jack/name.json?access_token=<TOKEN>'

即使我在设置它时使用了新的firebase控制台,即使我使用的令牌是使用新的Firebase sdk生成的,新的方式也不适用于我 . 有人知道为什么只有弃用的方式有效吗?我有兴趣将令牌放在我的请求的 Headers 中,但不能这样做 .

5 回答

  • -2

    对于java:我尝试使用auth来访问DatabaseRealtime . 跑:

    curl 'https://PROJECT_ID.firebaseio.com/users.json?auth=DATABASE_SECRET'
    

    它工作但这种方式已弃用 .
    之后我尝试使用access_token,当我使用access_token在我的firebase项目中查询数据库时遇到了问题 . 我已经找到了我之前遇到的错误的根本原因 . 因为我生成了access_token错误 . 我按步骤固定:

    1. add google-api-client into pom.xml

    <dependency>
          <groupId>com.google.api-client</groupId>
          <artifactId>google-api-client</artifactId>
          <version>1.22.0</version>
        </dependency>
    

    2. Get token

    public static void main(String [] args)throws Exception {
    GoogleCredential googleCred = GoogleCredential.fromStream(new
    的FileInputStream( “C://realtime.json”));
    GoogleCredential scoped = googleCred.createScoped(
    Arrays.asList(
    //或使用firebase.database.readonly进行只读访问
    “https://www.googleapis.com/auth/firebase.database”
    “https://www.googleapis.com/auth/userinfo.email”

    );
    scoped.refreshToken();
    String token = scoped.getAccessToken();
    的System.out.println(令牌);
    }

    3. try to access database 复制上面打印的值
    运行卷曲:

    卷曲'https://PROJECT_ID.firebaseio.com/users.json?access_token=TOKEN'

    它运作良好 .

    欲了解更多信息,请参阅链接:https://firebase.google.com/docs/reference/rest/database/user-auth#section-get

  • -1

    您需要将access_token放在标头中 .

    Headers 名称:授权 Headers 内容:Bearer the_token

    要尝试并添加一些 Headers ,您可以使用一些工具,如postman for google chrome或其他工具 .

  • 1

    像这样:

    try (InputStream is = new ClassPathResource("your-admin-info.json").getInputStream()) {
    
          GoogleCredential googleCred = GoogleCredential.fromStream(is);
          scoped = googleCred.createScoped(
              Arrays.asList(
                  "https://www.googleapis.com/auth/firebase.database",
                  "https://www.googleapis.com/auth/userinfo.email"
              )
          );
          scoped.refreshToken();
          scoped.getAccessToken();
    

    your-admin-info.json是您可以在帐户上生成的服务管理员帐户信息

  • 2

    我有同样的问题 . 仅在Authorization标头中添加令牌不起作用,但在请求中包含'auth ='的旧方法有效 . 我认为这可能是Firebase中的一个错误 .

    我的解决方案是使用新方式和旧方法,即在请求中包括'auth ='以及Authorization标头中的标记 . 因此,在他们解决问题后,您的应用将继续运行 .

    顺便说一下那些关于规则的答案是不正确的,如果问题是由规则引起的,那么错误将是401未经授权而不是403而消息'权限被拒绝'

  • 0

    您需要检查规则是否配置正确 .

    验证有4个选项可供使用 .

    4 options to use to Authenticate in firebase

    需要为每个选项配置不同的规则 .

    请尝试以下规则:

    Firebase Rule

相关问题