如何通过HTTP REST API访问我的Firebase数据库? [英] How do I access my Firebase Database via HTTP REST API?

查看:530
本文介绍了如何通过HTTP REST API访问我的Firebase数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢此答案我可以通过HTTP REST API和电子邮件/密码连接到Firebase 3。使用此API登录会返回用于访问Firebase数据库的访问令牌。此访问令牌在1小时后过期。登录后还会返回一个刷新令牌,我可以使用该令牌刷新我的访问令牌。这是我特别做的:

方法:

$ $ p $ POST

网址:

  https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=<my-firebase-api-key> 

有效负载:

 < code $ {
email:< email>,
password:< password>,
returnSecureToken:true

回应:

  { 
kind:identitytoolkit#VerifyPasswordResponse,
localId:< firebase-user-id>,//用来唯一标识用户
email: < email>,
displayName:,
idToken:< provider-id-token>,//将其用作数据库请求中的认证令牌
registered:true,
refreshToken:< refresh-token>,
expiresIn:3600
}

在刷新我的访问令牌的情况下:

URL:

b
$ b

  https://securetoken.googleapis.com/v1/token?key=<my-firebase-api-key> 

有效负载:

  {
grant_type:refresh_token,
refresh_token:< refresh-token>

$ / code $ / pre

$ p

$ $
access_token:< access-token>,
expires_in:3600,
token_type:持票人,
refresh_token:< refresh-token>,
id_token:< id-token>,
user_id:< user-id> ;,
project_id:< project-id>





如何通过HTTP REST API访问我的数据库有我的访问令牌?

所以在与技术支持沟通后,我的答案是:$ / $> $ / $>

在你的数据库规则中,包含一些类似于你正在做的事情的东西:

 <$ c 
rules:{
users:{
$ user_id:{
//授予对此用户帐户所有者的写入权限
//其uid必须与该键完全匹配($ user_id)
.write:$ user_id === auth.uid,
.read:$ user_id === auth.uid

}
}
}



在你的数据库中,创建一个 users 表,并在其中创建一个名为< user-id> 您正在使用的验证电子邮件/密码帐户。在该表中,您可以通过 access-key 访问该信息。然后发送这样的请求:

  https://  samplechat.firebaseio-demo.com/users/<user-id>.json?auth=<access-key> 

其中 access-key 是关键可以被称为 idToken id_Token access_key in来自Google的JSON响应。

Thanks to this answer I am able to connect to Firebase 3 via HTTP REST API and an email/password. Logging in with this API returns an access token that is used to access the Firebase Database. This access token expires after 1 hour. A refresh token is also returned after logging in, which I can use to refresh my access token. Here is what I am doing specifically:

Method:

POST

URL:

https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=<my-firebase-api-key>

Payload:

{
    email: "<email>",
    password: "<password>",
    returnSecureToken: true
}

Response:

{
    "kind": "identitytoolkit#VerifyPasswordResponse",
    "localId": "<firebase-user-id>", // Use this to uniquely identify users
    "email": "<email>",
    "displayName": "",
    "idToken": "<provider-id-token>", // Use this as the auth token in database requests
    "registered": true,
    "refreshToken": "<refresh-token>",
    "expiresIn": "3600"
}

In the case of refreshing my access token:

URL:

https://securetoken.googleapis.com/v1/token?key=<my-firebase-api-key>

Payload:

{
    grant_type: "refresh_token",
    refresh_token: "<refresh-token>"
}

Response:

{
  "access_token": "<access-token>",
  "expires_in": "3600",
  "token_type": "Bearer",
  "refresh_token": "<refresh-token>",
  "id_token": "<id-token>",
  "user_id": "<user-id>",
  "project_id": "<project-id>"
}

How do I access my database via HTTP REST API given that I have my access token?

解决方案

So after communicating with technical support, here's my answer:

In your database rules, include something like this that is compatible with what you're doing:

{
"rules": {
"users": {
"$user_id": {
// grants write access to the owner of this user account
// whose uid must exactly match the key ($user_id)
".write": "$user_id === auth.uid",
".read": "$user_id === auth.uid"
}
    }
  } 
}

And in your database, create a users table, and within that, create a table with the name of your <user-id> of the authentication email/password account you are using. Within that table is the information you will be able to access via your access-key.

Then send a request like this:

https://samplechat.firebaseio-demo.com/users/<user-id>.json?auth=<access-key>

Where access-key is the key that can be known as idToken, id_Token, or access_key in JSON responses from Google.

这篇关于如何通过HTTP REST API访问我的Firebase数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆