2013-03-26 219 views
3

我正在創建一個可以將文件插入Google雲端硬盤並將其列出的應用程序。我正在使用Google Drive SDK v2 API。但我的問題是它沒有列出沒有通過我的應用程序上傳的文件。如果我直接從Google驅動器上傳,它不會列在我的應用程序中。Google雲端硬盤SDK API未列出Google雲端硬盤上傳的文件

這裏列出文件的方法:

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; 
     } 

,我遍歷文件是這樣的:

List<File> files = retrieveAllFiles(service); 
     for(File f : files) { 
      System.out.println("File Name : "+f.getOriginalFilename(); 
     } 

誰能幫助我嗎?在此先感謝...

+0

我有同樣的問題。你能告訴我如何解決它?我與它連接數天 – Dolphin 2013-07-19 09:03:36

+0

更改oauth範圍到'https:// www.googleapis.com/auth/drive' – 2013-07-19 09:17:00

+0

我正在使用java android。我不知道在哪裏改變這個範圍。這裏是我的代碼,你可以看看http://pastebin.com/pTFaVs4A – Dolphin 2013-07-19 09:29:19

回答

2

我認爲你使用錯誤的oauth範圍,可能https://www.googleapis.com/auth/drive.file限制你的應用程序訪問由您的應用程序創建或打開的文件,當你應該使用https://www.googleapis.com/auth/drive,它可以完全控制你的應用程序。

+0

是的,這是正確的...我是新的谷歌驅動器SDK ...謝謝你的回答... – 2013-04-06 12:06:58

+1

@PrabhatSubedi:Google最近有什麼變化...改變建議的範圍仍然只列出由我的應用程序創建的文件。 http://stackoverflow.com/questions/27759077/drive-sdk-not-listing-all-my-files – 2015-01-06 03:44:35

+0

@llamawithabowlcut目前無法在Google雲端硬盤中工作...更好地查看SDK的文檔和版本。 – 2015-01-06 05:19:38

0

我構建了我們都在尋找的功能。以下列出了Google雲端硬盤文件夾中的所有文件和文件夾。您可以指定是否需要目錄中的所有文件,或者只需要該目錄中服務擁有的文件。天氣你只需要id,或者如果你只想要文件名或者如果你想要兩個。

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 
+0

我無法刪除我自己的答案 - 這屬於不同的頁面:http://stackoverflow.com/questions/11730454/clarification-on-maxresults-and-nextpagetoken-using-google-drive-api-v2/39417284 #39417284 – 2016-09-09 18:01:42

+0

此解決方案也是python--增加了它的無關性 – 2016-09-09 18:02:18

相關問題