列出文件的标签

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

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

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

示例

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

Java

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

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