Return a label from a file resource
Stay organized with collections
Save and categorize content based on your preferences.
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 labelInfo
object 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
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-28 UTC.
[null,null,["Last updated 2025-08-28 UTC."],[],[],null,["# Return a label from a file resource\n\nThis page describes how to return specific labels from a Google Drive file\nresource.\n\nTo specify which labels that you want to retrieve, use the\n[`files.get`](/workspace/drive/api/v2/reference/files/get) method or any method that\nreturns a [file resource](/workspace/drive/labels/reference/rest/v2/labels). The request\nbody must be empty.\n\nIf successful, the [response\nbody](/workspace/drive/api/reference/rest/v2/files/get#response-body) contains an instance\nof [`File`](/workspace/drive/api/reference/rest/v2/files#File).\n\nExample\n-------\n\nThe following code sample shows how to use the `fileId`, plus the `labelId`, to\nreturn the set of specific labels. The\n[`includeLabels`](/workspace/drive/api/reference/rest/v2/files/get#query-parameters)\nobject is a comma-separated list of IDs. The `labelInfo`object in the `fields`\nparameter contains labels set on the file and requested within `includeLabels`. \n\n### Java\n\n File file = driveService.files().get(\"\u003cvar translate=\"no\"\u003eFILE_ID\u003c/var\u003e\").setIncludeLabels(\"\u003cvar translate=\"no\"\u003eLABEL_ID\u003c/var\u003e,\u003cvar translate=\"no\"\u003eLABEL_ID\u003c/var\u003e\").setFields(\"labelInfo\").execute();\n\n### Python\n\n file = drive_service.files().get(fileId=\"\u003cvar translate=\"no\"\u003eFILE_ID\u003c/var\u003e\", includeLabels=\"\u003cvar translate=\"no\"\u003eLABEL_ID\u003c/var\u003e,\u003cvar translate=\"no\"\u003eLABEL_ID\u003c/var\u003e\", fields=\"labelInfo\").execute();\n\n### Node.js\n\n /**\n * Get a Drive file with specific labels\n * @return{obj} file with labelInfo\n **/\n async function getFileWithSpecificLabels() {\n // Get credentials and build service\n // TODO (developer) - Use appropriate auth mechanism for your app\n\n const {GoogleAuth} = require('google-auth-library');\n const {google} = require('googleapis');\n\n const auth = new GoogleAuth({scopes: 'https://www.googleapis.com/auth/drive'});\n const service = google.drive({version: 'v3', auth});\n try {\n const file = await service.files.get({\n fileId: '\u003cvar translate=\"no\"\u003eFILE_ID\u003c/var\u003e',\n includeLabels: '\u003cvar translate=\"no\"\u003eLABEL_ID\u003c/var\u003e,\u003cvar translate=\"no\"\u003eLABEL_ID\u003c/var\u003e',\n fields:'labelInfo',\n });\n return file;\n } catch (err) {\n // TODO (developer) - Handle error\n throw err;\n }\n }\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eFILE_ID\u003c/var\u003e: The `fileId` of the file containing the labels.\n- \u003cvar translate=\"no\"\u003eLABEL_ID\u003c/var\u003e: The `labelId` of a label to return. To locate the labels on a file, use the [`files.listLabels`](/workspace/drive/api/v2/reference/files/listLabels) method.\n\nNotes\n-----\n\n- Any method returning a [file\n resource](/workspace/drive/labels/reference/rest/v2/labels) supports the `includeLabels` field and query parameter. For example, [`files.copy`](/workspace/drive/api/reference/rest/v2/files/copy), [`files.list`](/workspace/drive/api/reference/rest/v2/files/list), and [`files.update`](/workspace/drive/api/reference/rest/v2/files/update)."]]