搜尋標籤

貴機構可以設定多個標籤,其中包含多個欄位的標籤。Labels API 提供 labels 集合來用於讀取標籤。

本頁說明如何搜尋並擷取標籤。

方法

labels 集合提供下列讀取標籤值的方法,每個方法都有特定工作:

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

依據資源名稱取得標籤

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

必須提供標籤資源名稱,結構可以是:

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

您也必須指定:

  • LabelViewLABEL_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

name = "labels/NAME@published"

# Label view controls level of data in response
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);
});

列出所有標籤

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

您也必須指定:

  • 用於將清單要求範圍限定為 customer。如未設定 customer,系統會傳回目前客戶中的所有標籤。

  • LabelViewLABEL_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');
  }
});