【问题标题】:detect changes in google drive list returning 0 but for same pagetoken returns change list with api检测返回 0 的谷歌驱动器列表中的更改,但对于相同的 pagetoken 使用 api 返回更改列表
【发布时间】:2017-06-11 23:19:30
【问题描述】:

创建将捕获 Google Drive 更改的 Java 应用程序,并将 Java 客户端用于 Google Drive V3 API。下面的代码显示了我们如何调用 Changes.List 方法来返回驱动器更改的列表。

https://developers.google.com/drive/v3/reference/changes/list 跟随此页面令牌 3411 给出列表

    {
    "kind": "drive#changeList",
    "newStartPageToken": "3420",
    "changes": [
    {
    "kind": "drive#change",
    "type": "file",
    "time": "2017-06-11T10:23:44.740Z",
    "removed": false,
    "fileId": "0B5nxCVMvw6oHaGNXZnlIb1I1OEE",
    "file": {
    "kind": "drive#file",
    "id": "0B5nxCVMvw6oHaGNXZnlIb1I1OEE",
    "name": "NewsLetters",
    "mimeType": "application/vnd.google-apps.folder"
   }
   },
   {
  "kind": "drive#change",
  "type": "file",
  "time": "2017-06-11T10:23:49.982Z",
  "removed": false,
  "fileId": "0B5nxCVMvw6oHeWdTYzlsOWpFOEU",
  "file": {
  "kind": "drive#file",
  "id": "0B5nxCVMvw6oHeWdTYzlsOWpFOEU",
  "name": "Copy of Copy of learning11.txt",
  "mimeType": "text/plain"
 }
 },

但是通过使用代码

      AppIdentityCredential credential= new  
   AppIdentityCredential(Collections.singleton(DriveScopes.DRIVE_METADATA));
        driveService = new Drive.Builder(
                HTTP_TRANSPORT_REQUEST, JSON_FACTORY, credential)
                .setApplicationName(APPLICATION_NAME)
                .build();
 String pageToken = "3411";
 while (pageToken != null) {
 ChangeList changes = driveService.changes().list(pageToken)
        .execute();
  Log.info("changes.getChanges 3411 "+changes.getChanges().size());

 for (Change change : changes.getChanges()) {
    // Process change
    System.out.println("Change found for file: " + change.getFileId());
 }
if (changes.getNewStartPageToken() != null) {
    // Last page, save this token for the next polling interval
    savedStartPageToken = changes.getNewStartPageToken();
}
pageToken = changes.getNextPageToken(); 
}

它给了

      Log.info("changes.getChanges 3411 "+changes.getChanges().size());

大小返回 0 即使我尝试过

     driveService.changes().list("3411"). setFields("changes").execute()

同样的结果 0 我正在使用 AppEngine 谷歌云服务器。 我想获取 folderID 的更改列表。 我在做什么错误。任何指针。请帮忙。

这是因为 Google Drive API through Google App Engine

由于其安全模型,Drive SDK 不支持服务帐户。 App Identity 不适用于 Drive API。不会是bug吧

但使用 AppIdentity 我可以读取文件夹中的文件

   result = service.files().list().setQ("'" + locdriveFolderID + "' in 
   parents")
                .setPageSize(10)
                .setFields("nextPageToken, files(id, 
   name,description,mimeType,modifiedTime)")
                .setOrderBy("modifiedTime")
                .execute();

为什么 changes.getChanges() 返回 0 它应该返回由 api >1 显示的更改列表。

请让我更正。

【问题讨论】:

  • 如何通过 AppIdentityCredential 从 serviceAccount 获取更改列表

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


【解决方案1】:

changes.getChanges()返回列表if的结果

   AuthorizationCodeFlow authFlow = initializeFlow();
   Credential credential = authFlow.loadCredential(getUserId(req));
   driveService = new Drive.Builder(
            HTTP_TRANSPORT_REQUEST, JSON_FACTORY, credential)
            .setApplicationName(APPLICATION_NAME)
            .build();

    ChangeList changes = mService.changes().list("3411").execute();
        Log.info("changes.getChanges "+changes.getChanges().size() );

日志输出

changes.getChanges 10

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多