ファイルのラベルを一覧表示する

組織には複数のラベルを設定できます。ラベルには任意の数のフィールドがあります。このページでは、1 つの 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 に置き換えます。