Google Drive API v3从GAE标准访问FileList

时间:2019-02-16 22:09:14

标签: java google-app-engine google-drive-api

我正在尝试从Drive文件夹中读取文件列表。我已经为该项目创建了一个服务帐户,并向api访问范围为https://www.googleapis.com/auth/drive的G-Suite域添加了api客户端访问权限。

这是从各种来源收集来的代码,

HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
AppIdentityCredential credential = 
   new AppIdentityCredential.Builder(Arrays.asList(DriveScopes.DRIVE)).build();
// API_KEY is from the Google Console as a server API key
GoogleClientRequestInitializer keyInitializer = 
   new CommonGoogleClientRequestInitializer("<my-service-account-key>");
Drive service = new Drive.Builder(httpTransport, jsonFactory, null)
   .setHttpRequestInitializer(credential)
   .setGoogleClientRequestInitializer(keyInitializer)
   .setApplicationName("<my-service-account-name>").build();     

String pageToken = null;
do {
   StringBuilder query = new StringBuilder(
                "'" + folderId + "' in parents and mimeType != 'application/vnd.google-apps.folder' and trashed = false");
   FileList result = service.files().list()
      .setQ(query.toString())
      .setSpaces("drive")
      .setFields("nextPageToken, files(id, name)")
      .setPageToken(pageToken).execute();
   // do something
} while (pageToken != null);

更新我将代码部署到了appengine标准,并且它无例外运行,只是它记录了“未找到文件”。当我将查询更改为'root'时,它返回找到的1个文件“ Getting starting()”,奇怪的是,我无权访问它,并且在我请求访问时也没有收到电子邮件; )

当前正在调查查询...查询看起来还可以,所以我如何访问文件夹以获取FileList?

0 个答案:

没有答案