列出文件的标签

您的组织可以拥有多个标签,每个标签可以包含任意数量的字段。本页介绍了如何列出单个 Google 云端硬盘文件上的所有标签。

如需列出文件标签,请使用 files.listLabels 方法。请求正文必须为空。该方法还接受可选的查询参数 maxResults,用于设置每页返回的标签数量上限。如果未设置,则返回 100 个结果。

如果成功,响应正文会包含应用于文件的标签列表。这些对象存在于类型为 Labelitems 对象中。

示例

以下代码示例展示了如何使用标签的 fileId 来检索正确的标签。

Java

List<Label> labelList = labelsDriveClient.files()
    .listLabels("FILE_ID")
    .execute()
    .getLabels();

Python

label_list_response = drive_service.files().listLabels(
    fileId="FILE_ID"
).execute()

Node.js

/**
 * Lists all the labels on a Drive file
 * @return{obj} a list of Labels
 **/
async function listLabels() {
  // Get credentials and build service
  // TODO (developer) - Use appropriate auth mechanism for your app

  const {GoogleAuth} = require('google-auth-library');
  const {google} = require('googleapis');

  const auth = new GoogleAuth({
    scopes: 'https://www.googleapis.com/auth/drive',
  });
  const service = google.drive({version: 'v3', auth});
  try {
    const labelListResponse = await service.files.listLabels({
      fileId: 'FILE_ID',
    });
    return labelListResponse;
  } catch (err) {
    // TODO (developer) - Handle error
    throw err;
  }
}

FILE_ID 替换为您要获取标签列表的文件的 fileId