web-dev-qa-db-ja.com

HTTP REST API経由でFirebaseデータベースにアクセスするにはどうすればよいですか?

この回答 のおかげで、HTTP REST APIとメール/パスワードを介してFirebase 3に接続できます。このAPIでログインすると、使用されるアクセストークンが返されますFirebase Databaseにアクセスするには、このアクセストークンは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にアクセストークンがある場合)

13
GShocked

テクニカルサポートに連絡した後、ここに私の答えがあります。

データベースルールに、実行していることと互換性のある次のようなものを含めます。

{
"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テーブルを作成し、その中に、使用している認証メール/パスワードアカウントの<user-id>の名前でテーブルを作成します。そのテーブル内には、access-key経由でアクセスできる情報があります。

次に、次のようなリクエストを送信します。

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

ここで、access-keyは、GoogleからのJSON応答でidTokenid_Token、またはaccess_keyとして認識できるキーです。

14
GShocked