列出檔案的標籤

貴機構可以擁有多個標籤,且標籤可包含任意數量的欄位。本頁面說明如何列出單一 Google 雲端硬碟檔案的所有標籤。

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

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

範例

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