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

組織には複数のラベルを設定でき、ラベルには任意の数のフィールドを設定できます。このページでは、単一の Google ドライブ ファイルのすべてのラベルを一覧表示する方法について説明します。

ファイルラベルを一覧表示するには、 files.listLabels メソッドを使用します。リクエスト本文は空にする必要があります。このメソッドは、オプションのクエリ パラメータ maxResults を使用して、ページごとに返されるラベルの最大数を設定することもできます。設定しない場合は、100 件の結果が返されます。

成功した場合、レスポンス の本文にはファイルに適用された ラベルのリストが含まれます。これらは、items タイプの Label オブジェクト内に存在します。

次のコードサンプルは、ラベルの 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 に置き換えます。