搜尋標籤

貴機構可以有多個標籤,每個標籤都有多個欄位。 Google 雲端硬碟 Labels API 提供 labels 資源,可啟用標籤讀取功能。

本文說明如何使用 Drive Labels API 搜尋及擷取標籤。

方法

labels 資源提供下列方法來讀取標籤值,每種方法都有特定用途:

範圍 方法
依資源名稱顯示單一標籤 labels.get
所有標籤 labels.list

依資源名稱取得標籤

如要依資源名稱取得單一標籤,請使用 labels 資源上的 get 方法。

必須提供標籤資源 name,結構如下:

  • labels/{id}labels/{id}@latest:取得最新標籤修訂版本。
  • labels/{id}@published:取得目前發布的標籤修訂版本。
  • labels/{id}@{revisionId}:取得指定修訂版本 ID 的標籤。

您必須指定 LabelView 物件做為 LABEL_VIEW_FULL,才能設定套用至標籤回應的資源檢視畫面。LABEL_VIEW_FULL 會傳回所有可能的欄位。

以下程式碼範例說明如何使用標籤的 name,依資源名稱取得單一標籤:

Python

# Label name, with or without revision:
#
# Revision specified:
# labels/LABEL_ID@published
# labels/LABEL_ID@latest
# labels/LABEL_ID@1
#
# No revision specified, returns latest revision:
# labels/LABEL_ID
name = 'labels/NAME@published'

# Label view controls level of data in response
view = 'LABEL_VIEW_FULL'

label = service.labels().get(
    name=name,
    view=view
).execute()

Node.js

// Label name, with or without revision:
//
// Revision specified:
// labels/LABEL_ID@published
// labels/LABEL_ID@latest
// labels/LABEL_ID@1
//
// No revision specified, returns latest revision:
// labels/LABEL_ID
const name = 'labels/NAME@published';

// Label view controls level of data in response
const view = 'LABEL_VIEW_FULL';

service.labels.get({
  name: name,
  view: view
}, (err, res) => {
  if (err) return console.error('The API returned an error: ' + err);
  console.log(res);
});

NAME 替換為標籤名稱。

列出所有標籤

如要取得標籤清單,請使用 labels 資源的 list 方法。

此外,您還必須指定:

  • customer 查詢參數,用於將這項清單要求限定在特定範圍。如果未設定 customer,系統會傳回目前客戶中的所有標籤。

  • LabelView 物件 (如 LABEL_VIEW_FULL),用於設定套用至標籤回應的資源檢視畫面。LABEL_VIEW_FULL 會傳回所有可能的欄位。

下列程式碼範例說明如何使用 customer 查詢參數取得標籤清單:

Python

response = service.labels().list(
    customer='customers/CUSTOMER',
    view='LABEL_VIEW_FULL'
).execute()

Node.js

const params = {
  customer: 'customers/CUSTOMER',
  view: 'LABEL_VIEW_FULL'
};

service.labels.list(params, (err, res) => {
  if (err) return console.error('The API returned an error: ' + err);
  const labels = res.data.labels;
  if (labels) {
    labels.forEach((label) => {
      const name = label.name;
      const title = label.properties.title;
      console.log(`${name}\t${title}`);
    });
  } else {
    console.log('No Labels');
  }
});

CUSTOMER 替換為客戶名稱。