貴機構可以擁有多個標籤,以及多個欄位的標籤。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');
}
});