貴機構可以有多個標籤,每個標籤都有多個欄位。
Labels API 提供 labels 集合,可啟用標籤讀取功能。
本頁面說明如何搜尋及擷取標籤。
方法
labels 集合提供下列方法來讀取標籤值,每個方法都有特定用途:
| 範圍 | 閱讀 | 
|---|---|
| 依資源名稱取得單一標籤 | labels.get | 
| 所有標籤 | labels.list | 
依資源名稱取得標籤
如要依資源名稱取得單一標籤,請使用 labels.get 方法。
必須提供標籤資源名稱,結構如下:
- 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
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,系統會傳回目前客戶的所有標籤。
- 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');
  }
});