【问题标题】:Google Drive API - update file metadata onlyGoogle Drive API - 仅更新文件元数据
【发布时间】:2017-08-16 12:43:38
【问题描述】:

我正在尝试重命名 google 驱动器文件资源。我想我只是遗漏了一些东西,因为所有其他操作(例如获取文件列表、插入文件、在目录之间移动文件)都在工作。

前提条件:尝试使用此文档 https://developers.google.com/drive/v2/reference/files/update 和 java 重命名文件资源(仅使用 JDK 内容)。另外,我不使用 gdrive java sdk、apache http 客户端或其他库......只是清理 JDK 工具。

那么我该怎么做:

  1. Here 是我要发送的文件元数据。

  2. 修改此元数据中的title 属性

  3. 代码如下:

    URLConnection urlConnection = new URL("https://www.googleapis.com/drive/v2/files/" + fileId).openConnection();
    
    if (urlConnection instanceof HttpURLConnection) {
        HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
        httpURLConnection.setRequestMethod("PUT");
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setRequestProperty("Authorization", "Bearer " + accessToken);
    
        DataOutputStream outputStream = new DataOutputStream(httpURLConnection.getOutputStream());
        outputStream.writeBytes(FILE_RESOURCE_METADATA_WITH_CHANGED_TITLE_IN_JSON);
        outputStream.flush();
        outputStream.close();
    }
    

在对 API 进行实际调用后,我在响应正文中收到 200 个状态代码和文件资源(如预期的那样),但标题保持不变。所以我没有错误没有更改标题。

此外,google drive api 会忽略文件资源中的任何更改。它只是返回相同的文件资源而不应用任何更改(尝试使用标题、描述、原始文件名、父属性)。

到目前为止我也尝试过:

  1. 只发送应该改变的属性,比如

    {"title":"some_new_name"}
    

结果是一样的。

  1. PUT 更改为PATCH。不幸的是,PATCH 不受 HttpURLConnection 支持,但解决方法给出了相同的结果。更改将被忽略。

  2. 使用了 google api exlorer(可以在 API 参考页面的右侧找到) - 并且...它可以工作。仅在请求正文中填写了 fileId 和 title 属性,并且可以正常工作。文件已重命名。

我错过了什么?

【问题讨论】:

    标签: google-api google-drive-api google-apis-explorer


    【解决方案1】:

    找到解决方案...

    添加这个请求属性解决了这个问题。

    httpURLConnection.setRequestProperty("Content-Type", "application/json")
    

    【讨论】:

      【解决方案2】:

      试试documentation中给出的示例java代码。

      由于代码处理更新现有文件的元数据和内容。

      从代码中,您会找到file.setTitle(newTitle),我认为这是您想要实现的。

      import com.google.api.client.http.FileContent;
      import com.google.api.services.drive.Drive;
      import com.google.api.services.drive.model.File;
      
      import java.io.IOException;
      // ...
      
      public class MyClass {
      
        // ...
      
        /**
         * Update an existing file's metadata and content.
         *
         * @param service Drive API service instance.
         * @param fileId ID of the file to update.
         * @param newTitle New title for the file.
         * @param newDescription New description for the file.
         * @param newMimeType New MIME type for the file.
         * @param newFilename Filename of the new content to upload.
         * @param newRevision Whether or not to create a new revision for this
         *        file.
         * @return Updated file metadata if successful, {@code null} otherwise.
         */
        private static File updateFile(Drive service, String fileId, String newTitle,
            String newDescription, String newMimeType, String newFilename, boolean newRevision) {
          try {
            // First retrieve the file from the API.
            File file = service.files().get(fileId).execute();
      
            // File's new metadata.
            file.setTitle(newTitle);
            file.setDescription(newDescription);
            file.setMimeType(newMimeType);
      
            // File's new content.
            java.io.File fileContent = new java.io.File(newFilename);
            FileContent mediaContent = new FileContent(newMimeType, fileContent);
      
            // Send the request to the API.
            File updatedFile = service.files().update(fileId, file, mediaContent).execute();
      
            return updatedFile;
          } catch (IOException e) {
            System.out.println("An error occurred: " + e);
            return null;
          }
        }
      
        // ...
      }
      

      希望这能给你加分。

      【讨论】:

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