驗證 REST 要求

Firebase SDK 會處理與 Firebase 即時資料庫代您建立。不過,如果您在 不具有用戶端 SDK 的環境,或是不為了讓 建立永久資料庫連線 用於讀取及寫入資料的即時資料庫 REST API。

請透過下列其中一種方法驗證使用者:

  1. Google OAuth2 存取權杖 - 可以讀取及讀取 寫入即時資料庫的資料 即時資料庫規則。不過,您可以將自己的資料 並授予該伺服器對 並透過 Google OAuth2 存取權杖取得資料。

  2. Firebase ID 符記:您也可以傳送已驗證的要求 也能讀取個別使用者的資料,例如透過即時資料庫規則 用戶端 SDK。REST API 會接受 用戶端 SDK。

Google OAuth2 存取權杖

可依據您的 您也能讀取及寫入即時資料庫規則 取得任何驗證資訊不過,如果您想讓伺服器 如要略過即時資料庫規則,您必須驗證讀取和寫入作業 要求。透過 Google OAuth2 進行驗證需要執行以下步驟:

  1. 產生存取權杖。
  2. 驗證該存取權杖。

產生存取權杖

即時資料庫 REST API 接受 標準 Google OAuth2 存取權杖。 您可使用適當的服務帳戶產生存取權杖 可存取即時資料庫的權限。按一下「產生新的私人訊息」 畫面底部的鑰匙按鈕 服務帳戶 可透過 Firebase 控制台的專區,輕鬆產生新的服務 。

取得服務帳戶金鑰檔案後,您可以使用 Google API 用戶端程式庫 產生具備下列必要範圍的 Google OAuth2 存取權杖:

  • https://www.googleapis.com/auth/userinfo.email
  • https://www.googleapis.com/auth/firebase.database

以下提供幾個執行 Google OAuth2 建立方式的範例。 存取權杖,以便向即時資料庫 REST API 進行驗證 多個語言

Node.js

針對 Google API 用戶端程式庫 Node.js

var {google} = require("googleapis");

// Load the service account key JSON file.
var serviceAccount = require("path/to/serviceAccountKey.json");

// Define the required scopes.
var scopes = [
  "https://www.googleapis.com/auth/userinfo.email",
  "https://www.googleapis.com/auth/firebase.database"
];

// Authenticate a JWT client with the service account.
var jwtClient = new google.auth.JWT(
  serviceAccount.client_email,
  null,
  serviceAccount.private_key,
  scopes
);

// Use the JWT client to generate an access token.
jwtClient.authorize(function(error, tokens) {
  if (error) {
    console.log("Error making request to generate access token:", error);
  } else if (tokens.access_token === null) {
    console.log("Provided service account does not have permission to generate access tokens");
  } else {
    var accessToken = tokens.access_token;

    // See the "Using the access token" section below for information
    // on how to use the access token to send authenticated requests to
    // the Realtime Database REST API.
  }
});

Java

針對 Google API 用戶端程式庫 Java

// Load the service account key JSON file
FileInputStream serviceAccount = new FileInputStream("path/to/serviceAccountKey.json");

// Authenticate a Google credential with the service account
GoogleCredential googleCred = GoogleCredential.fromStream(serviceAccount);

// Add the required scopes to the Google credential
GoogleCredential scoped = googleCred.createScoped(
    Arrays.asList(
      "https://www.googleapis.com/auth/firebase.database",
      "https://www.googleapis.com/auth/userinfo.email"
    )
);

// Use the Google credential to generate an access token
scoped.refreshToken();
String token = scoped.getAccessToken();

// See the "Using the access token" section below for information
// on how to use the access token to send authenticated requests to the
// Realtime Database REST API.

Python

使用 google-auth 程式庫:

from google.oauth2 import service_account
from google.auth.transport.requests import AuthorizedSession

# Define the required scopes
scopes = [
  "https://www.googleapis.com/auth/userinfo.email",
  "https://www.googleapis.com/auth/firebase.database"
]

# Authenticate a credential with the service account
credentials = service_account.Credentials.from_service_account_file(
    "path/to/serviceAccountKey.json", scopes=scopes)

# Use the credentials object to authenticate a Requests session.
authed_session = AuthorizedSession(credentials)
response = authed_session.get(
    "https://<DATABASE_NAME>.firebaseio.com/users/ada/name.json")

# Or, use the token directly, as described in the "Authenticate with an
# access token" section below. (not recommended)
request = google.auth.transport.requests.Request()
credentials.refresh(request)
access_token = credentials.token

使用存取權杖驗證

如要將已驗證的要求傳送至即時資料庫 REST API,請將 上方產生的 Google OAuth2 存取權杖 Authorization: Bearer <ACCESS_TOKEN> 標頭或 access_token=<ACCESS_TOKEN> 查詢字串參數。以下是 curl 的範例 要求讀取 Ada 的名稱:

curl "https://<DATABASE_NAME>.firebaseio.com/users/ada/name.json?access_token=<ACCESS_TOKEN>"

請務必將 <DATABASE_NAME> 換成 使用 Google OAuth2 存取權杖的即時資料庫和 <ACCESS_TOKEN>

要求成功後,會以 200 OK HTTP 狀態碼表示。 回應包含所擷取的資料:

{"first":"Ada","last":"Lovelace"}

Firebase ID 權杖

透過 Firebase 驗證功能登入使用者或裝置時,Firebase 會 可明確識別及授予使用者 例如即時資料庫和 Cloud Storage您可以重複使用 該 ID 權杖用於驗證即時資料庫 REST API,並向 。

產生 ID 權杖

如要從用戶端擷取 Firebase ID 符記,請按照下列步驟操作: 擷取用戶端上的 ID 權杖

請注意,ID 符記會在短時間內失效,因此應使用 擷取。

使用 ID 權杖進行驗證

如要將已驗證的要求傳送至即時資料庫 REST API,請將 在上方產生的 ID 權杖,做為 auth=<ID_TOKEN> 查詢字串參數。這裡 是讀取 Ada 名稱的 curl 請求範例:

curl "https://<DATABASE_NAME>.firebaseio.com/users/ada/name.json?auth=<ID_TOKEN>"

請務必將 <DATABASE_NAME> 換成 使用 Firebase ID 權杖的即時資料庫和 <ID_TOKEN>

要求成功後,會以 200 OK HTTP 狀態碼表示。 回應包含所擷取的資料:

{"first":"Ada","last":"Lovelace"}

舊版權杖

如果您目前仍使用舊版 Firebase 驗證權杖,建議您 將您的 REST 驗證更新為描述的其中一種驗證方法 。

Realtime Database REST API 仍支援透過舊版資料庫進行驗證 驗證權杖,包括密鑰。 如要查看即時資料庫密鑰,請前往 這個 服務帳戶 專區。

密鑰是長期憑證。建議您產生新的密鑰 移除具有密鑰存取權的使用者時,撤銷現有的帳戶 (例如 擁有者) 規則