您的组织可以有多个标签,每个标签都有多个字段。
Google 云端硬盘 Labels API 提供 labels 资源,以实现标签读取。
本文档介绍了如何使用 Drive Labels API 搜索和检索标签。
方法
labels 资源提供了以下用于读取标签值的方法,每种方法都有特定的用途:
| Range | 方法 |
|---|---|
| 按资源名称划分的单个标签 | 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 替换为客户的名称。