2013-03-07 52 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

相关问题