列出檔案的標籤

貴機構可以有多個標籤,其中的標籤數量不限。本頁面說明如何列出單一 Google 雲端硬碟檔案中的所有標籤。

如要列出檔案標籤,請使用 files.listLabels 方法。要求主體必須空白。這個方法也使用選用的查詢參數 maxResults,以設定每頁傳回的標籤數量上限。如未設定,系統會傳回 100 個結果。

如果成功,回應主體會包含套用至檔案的標籤清單。這些變數位於 Label 類型的 items 物件中。

範例

以下程式碼範例說明如何使用標籤的 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