Return a label from a file resource

This page describes how to return specific labels from a Google Drive file resource.

To specify which labels that you want to retrieve, use the files.get method or any method that returns a file resource. The request body must be empty.

If successful, the response body contains an instance of File.

Example

The following code sample shows how to use the fileId, plus the labelId, to return the set of specific labels. The includeLabels object is a comma-separated list of IDs. The labelInfoobject in the fields parameter contains labels set on the file and requested within includeLabels.

Java

File file = driveService.files().get("FILE_ID").setIncludeLabels("LABEL_ID,LABEL_ID").setFields("labelInfo").execute();

Python

file = drive_service.files().get(fileId="FILE_ID", includeLabels="LABEL_ID,LABEL_ID", fields="labelInfo").execute();

Node.js

/**
* Get a Drive file with specific labels
* @return{obj} file with labelInfo
**/
async function getFileWithSpecificLabels() {
  // 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 file = await service.files.get({
      fileId: 'FILE_ID',
      includeLabels: 'LABEL_ID,LABEL_ID',
      fields:'labelInfo',
    });
    return file;
  } catch (err) {
    // TODO (developer) - Handle error
    throw err;
  }
}

Replace the following:

  • FILE_ID: The fileId of the file containing the labels.
  • LABEL_ID: The labelId of a label to return. To locate the labels on a file, use the files.listLabels method.

Notes