6
votes

I'm currently using the Google Drive API to get a list of folders (or subfolders). In order to do this I use the following URL:

https://www.googleapis.com/drive/v2/files/1234folderid1234/children?key=1234APIKEY1234&q=mimeType='application/vnd.google-apps.folder';

This returns back a list of the folders as expected, however, I also need either the name of the folder or the full folder path. This information is not returned with this call. Is there a way to get the list of folders with the associated folder name or path in one call?

Thanks

2
This might answer your question. stackoverflow.com/questions/10345381/…Albert Fajardo
It looks like this question deals with v1 whereas I'm dealing with v2. Thanks though.Nick

2 Answers

8
votes

You need to do two things:

  1. Use files.list (which gives full file resources)
  2. Amend your query to add the parent id you want to search in.

    mimeType = 'application/vnd.google-apps.folder' and 'folderId' in parents

0
votes

I know it's late, but the best solution to solve this problem is to use a recursive function that searches for the last folder parent. There is a NodeJS solution with GD API v3, but the reasoning is the same for every language and GD API version.

const drive = google.drive({version: 'v3', auth});
const rootFolderID = 'TheHighestFolderIDOfYourGoogleDriveAccount';
function getFileOrFolderCompletePath(fileOrFolderID, pathList = [], callback){
    let pathList = [];

    if (fileOrFolderID !== rootFolderID){
        drive.files.get({
            fileId: fileID,
            fields: 'id, parents',
        }, (error, response) => {
            if (!error){
                pathList.push(response.data.parents[0])
                pathList = pathList.reverse();
                getFileOrFolderCompletePath(response.data.parents[0], pathList, callback);
            }
        });
    }else{
        callback(pathList.join('/'))
    }
}

You should call:

getFileOrFolderCompletePath('fileIDtoGetThePath', [], (path) => {
    console.log(path); //It will log: 'path/to/your/file'
})