2013-03-07 35 views
3

我正在嘗試編寫一個程序,使用Google Drive API在我的Google雲端硬盤中列出這些文件。谷歌驅動器文件列表API返回一個空的數組項目

我已爲我的帳戶創建了一個服務帳戶。

這裏是我一直在使用這個服務對象我叫的File.List

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(); 
      System.out.println(files); 
      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); 
    // System.out.println(result); 
    return result; 
} 

創建

public static Drive getDriveService() throws GeneralSecurityException, 
     IOException, URISyntaxException { 
    HttpTransport httpTransport = new NetHttpTransport(); 
    JacksonFactory jsonFactory = new JacksonFactory(); 
    GoogleCredential credential = new GoogleCredential.Builder() 
      .setTransport(httpTransport) 
      .setJsonFactory(jsonFactory) 
      .setServiceAccountId(SERVICE_ACCOUNT_EMAIL) 
      .setServiceAccountScopes(
        "https://www.googleapis.com/auth/drive") 
      .setServiceAccountPrivateKeyFromP12File(
        new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH)) 
      .build(); 
    Drive service = new Drive.Builder(httpTransport, jsonFactory, null) 
      .setApplicationName("FileListAccessProject") 
      .setHttpRequestInitializer(credential).build(); 

    return service; 
} 

服務對象,但是這是返回一個空的Items數組

{"etag":"\"8M2pZwE_fwroB5BIq5aUjc3uhqg/vyGp6PvFo4RvsFtPoIWeCReyIC8\"", 
"kind":"drive#fileList","selfLink":"https://www.googleapis.com/drive/v2/files", 
"items":[]} 

有人可以幫我這個,我錯過了什麼嗎?

謝謝。

回答

4

您正在使用application-owned account,它與常規帳戶相似,但屬於應用而不是用戶。

應用程序擁有的帳戶沒有特殊權限,並且與常規帳戶一樣只能訪問他們擁有的文檔或與他們共享的文檔。因此,如果您的常規帳戶擁有這些文件,則服務帳戶將無法列出它們。

您可以使用域範圍內的代表團,讓您的應用程序來訪問文件代表其他用戶在谷歌的Apps域:

https://developers.google.com/drive/delegation

+0

由於將嘗試了這一點。 – user1586121 2013-03-08 18:20:18

相關問題