Google Drive REST API:file.getCreatedTime() 始终返回 null

1 android google-api google-drive-api google-drive-android-api

我正在使用以下链接中提供的适用于 Google Drive Rest APi 的Android 快速入门安卓快速入门

示例代码按原样运行良好。但是,当我尝试从文件中获取其他详细信息时,例如getCreatedTime()GetWevViewLink()'null' 被返回。只有 getName() 和 getId() 返回值。

nig*_*ils 6

Google Drive REST APIs v3 would only return only certain default fields. If you need some field, you have to explicitly request it by setting it with .setFields() method.

Modify your code like this -

private List<String> getDataFromApi() throws IOException {
    // Get a list of up to 10 files.
    List<String> fileInfo = new ArrayList<String>();
    FileList result = mService.files().list()
         .setPageSize(10)

         // see createdTime added to list of requested fields
         .setFields("nextPageToken, files(createdTime,id,name)")                

         .execute();
    List<File> files = result.getFiles();
    if (files != null) {
        for (File file : files) {
            fileInfo.add(String.format("%s (%s)\n",
                    file.getName(), file.getId()));
        }
    }
    return fileInfo;
}
Run Code Online (Sandbox Code Playgroud)

You can read more about this behavior here https://developers.google.com/drive/v3/web/migration Updated link https://developers.google.com/drive/api/v2/migration

Quoting from the above link -

Notable changes

  • Full resources are no longer returned by default. Use the fields query parameter to request specific fields to be returned. If left unspecified only a subset of commonly used fields are returned.

Accept the answer if it works for you so that others facing this issue might also get benefited.