您的组织可以有多个标签,标签可以有多个字段。
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 替换为客户的名称。