如何通过 HTTP REST API 访问我的 Firebase 数据库?
How do I access my Firebase Database via HTTP REST API?
感谢 我能够通过 HTTP REST API 和 email/password 连接到 Firebase 3。使用此 API returns 访问令牌登录,用于访问 Firebase 数据库。此访问令牌在 1 小时后过期。登录后还会返回一个刷新令牌,我可以用它来刷新我的访问令牌。这是我正在做的具体事情:
方法:
POST
URL:
https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=<my-firebase-api-key>
有效负载:
{
email: "<email>",
password: "<password>",
returnSecureToken: true
}
回复:
{
"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"
}
在刷新我的访问令牌的情况下:
URL:
https://securetoken.googleapis.com/v1/token?key=<my-firebase-api-key>
有效负载:
{
grant_type: "refresh_token",
refresh_token: "<refresh-token>"
}
回复:
{
"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>"
}
如果我有访问令牌,如何通过 HTTP REST API 访问我的数据库?
所以在和技术支持沟通后,我的回答是:
在您的数据库规则中,包括与您正在做的事情兼容的类似内容:
{
"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"
}
}
}
}
然后在您的数据库中,创建一个 users
table,然后在其中创建一个 table,其中包含您的 <user-id>
身份验证 [=29] =] 您正在使用的帐户。 table 中包含您可以通过 access-key
访问的信息。
然后像这样发送一个请求:
https://samplechat.firebaseio-demo.com/users/<user-id>.json?auth=<access-key>
其中 access-key
是在 Google 的 JSON 响应中可以称为 idToken
、id_Token
或 access_key
的键.
感谢
方法:
POST
URL:
https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=<my-firebase-api-key>
有效负载:
{
email: "<email>",
password: "<password>",
returnSecureToken: true
}
回复:
{
"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"
}
在刷新我的访问令牌的情况下:
URL:
https://securetoken.googleapis.com/v1/token?key=<my-firebase-api-key>
有效负载:
{
grant_type: "refresh_token",
refresh_token: "<refresh-token>"
}
回复:
{
"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>"
}
如果我有访问令牌,如何通过 HTTP REST API 访问我的数据库?
所以在和技术支持沟通后,我的回答是:
在您的数据库规则中,包括与您正在做的事情兼容的类似内容:
{
"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"
}
}
}
}
然后在您的数据库中,创建一个 users
table,然后在其中创建一个 table,其中包含您的 <user-id>
身份验证 [=29] =] 您正在使用的帐户。 table 中包含您可以通过 access-key
访问的信息。
然后像这样发送一个请求:
https://samplechat.firebaseio-demo.com/users/<user-id>.json?auth=<access-key>
其中 access-key
是在 Google 的 JSON 响应中可以称为 idToken
、id_Token
或 access_key
的键.