I am creating an application which can insert files into Google Drive and lists it. I am using Google Drive SDK v2 API. But my problem is it is not listing files which is not uploaded through my application. If I upload directly from Google drive it is not listed in my application.
Here is the method to list the file:
private static List<File> retrieveAllFiles(Drive service) throws IOException {
List<File> result = new ArrayList<File>();
Files.List request = service.files().list();
do {
try {
FileList files = request.execute();
result.addAll(files.getItems());
request.setPageToken(files.getNextPageToken());
} catch (IOException e) {
System.out.println("An error occurred: " + e);
request.setPageToken(null);
}
} while (request.getPageToken() != null &&
request.getPageToken().length() > 0);
return result;
}
and I am iterating files like this :
List<File> files = retrieveAllFiles(service);
for(File f : files) {
System.out.println("File Name : "+f.getOriginalFilename();
}
Can anyone help me please ? Thanks in advance ...
I think you are using the wrong oauth scope, probably https://www.googleapis.com/auth/drive.file
which restrict your app's access to file created or opened by your app, when you should use https://www.googleapis.com/auth/drive
which gives full control to your app.
I built the function we were all really looking for. The following lists all the files and folders inside a google drive folder. You can specify if you want all the files in the directory OR just the files that the service owns inside the directory. Weather you want the ids only, or if you want the file names only or if you want both.
Library_GoogleDriveFileList:
import numpy
import pprint
from apiclient import errors
import collections
def Main(
Service = None,
FolderId = None,
ResultFileNamesOnly = False,
ResultFileIdsOnly = True,
IncludeServiceFilesOnly = False,
SortResults = False,
ResultFormat = None,
):
Result = None
if (ResultFormat is None):
ResultFormat = 'list'
if (FolderId == None):
FolderId = 'root'
SearchParameterString = "'" + FolderId + "' in parents"
SearchOwners = None
if (IncludeServiceFilesOnly):
SearchOwners = 'DEFAULT'
else:
SearchOwners = 'DOMAIN'
print 'SearchOwners', SearchOwners
#Because there is a return limit of 460 files (falsly documented as 1000)
# we need to loop through and grab a few at a time with different requests
DriveFileItems = []
PageToken = None
while True:
try:
print 'PageToken', PageToken
DriveFilesObject = Service.files().list(
q = SearchParameterString,
corpus = SearchOwners, #'DOMAIN'
maxResults = 200,
pageToken = PageToken,
).execute()
DriveFileItems.extend(DriveFilesObject['items'])
PageToken = DriveFilesObject.get('nextPageToken')
if not PageToken:
break
except errors.HttpError, error:
print 'An error occurred: %s' % error
break
#print 'DriveFileItems', DriveFileItems
#pprint.pprint(DriveFileItems)
FileNames = []
FileIds = []
for Item in DriveFileItems:
FileName = Item["title"]
FileNames.append(FileName)
FileId = Item["id"]
FileIds.append(FileId)
#print 'FileIds'
#pprint.pprint(FileIds)
if ResultFileNamesOnly == False and ResultFileIdsOnly == False:
if (ResultFormat == 'dict'):
Result = collections.OrderedDict()
for FileName , FileId in zip(FileNames, FileIds):
Result [FileName] = FileId
elif (ResultFormat == 'list'):
Result = numpy.array([FileIds, FileNames]).T.tolist()
elif ResultFileNamesOnly == True and ResultFileIdsOnly == False:
Result = FileNames
elif ResultFileNamesOnly == False and ResultFileIdsOnly == True:
Result = FileIds
elif ResultFileNamesOnly == True and ResultFileIdsOnly == True:
raise Exception('ResultFileNamesOnly == True and ResultFileIdsOnly == True:')
if ( ResultFormat == 'list'):#type(Result).__name__ == 'list'):
Result = list( reversed( Result ) )
if (SortResults):
Result = list( sorted( Result ) )
#TODO -> more intellegent sort for dict, 2d list
return Result