【问题标题】:Update file description in google drive using javascript使用javascript更新谷歌驱动器中的文件描述
【发布时间】:2017-01-29 16:57:02
【问题描述】:

我正在尝试更新我所有的谷歌驱动器图像描述。我可以访问谷歌驱动器,并且可以使用

获得结果
gapi.client.drive.files.list({
        'pageSize': 1000,
        '字段':'文件,nextPageToken',
        'q':查询,
        'orderBy':'名称',
        'access_token':访问令牌
    });

之后我想更新所有文件描述,但我得到了我需要登录的结果。我正在使用 OAuth 2.0 Playground 并获得所有驱动器的权限。

gapi.client.drive.files.update({
        'fileId': 文件id,
        'access_token':访问令牌,
        “资源”:身体
    });

知道如何登录或我做错了什么吗?

【问题讨论】:

    标签: google-api google-drive-api authorization


    【解决方案1】:

    尝试仔细检查您是否正确按照步骤here 使用 OAuth 授权您的请求,同时确保您在应用程序中使用正确的范围。这是link,它将向您解释每个作用域的含义和用途。

    现在要更新您的文件,这个documentation 可以帮助您。本文档的v2 version 有一个更新文件的 Javascript 代码示例。

    /**
     * Update an existing file's metadata and content.
     *
     * @param {String} fileId ID of the file to update.
     * @param {Object} fileMetadata existing Drive file's metadata.
     * @param {File} fileData File object to read data from.
     * @param {Function} callback Callback function to call when the request is complete.
     */
    function updateFile(fileId, fileMetadata, fileData, callback) {
      const boundary = '-------314159265358979323846';
      const delimiter = "\r\n--" + boundary + "\r\n";
      const close_delim = "\r\n--" + boundary + "--";
    
      var reader = new FileReader();
      reader.readAsBinaryString(fileData);
      reader.onload = function(e) {
        var contentType = fileData.type || 'application/octet-stream';
        // Updating the metadata is optional and you can instead use the value from drive.files.get.
        var base64Data = btoa(reader.result);
        var multipartRequestBody =
            delimiter +
            'Content-Type: application/json\r\n\r\n' +
            JSON.stringify(fileMetadata) +
            delimiter +
            'Content-Type: ' + contentType + '\r\n' +
            'Content-Transfer-Encoding: base64\r\n' +
            '\r\n' +
            base64Data +
            close_delim;
    
        var request = gapi.client.request({
            'path': '/upload/drive/v2/files/' + fileId,
            'method': 'PUT',
            'params': {'uploadType': 'multipart', 'alt': 'json'},
            'headers': {
              'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
            },
            'body': multipartRequestBody});
        if (!callback) {
          callback = function(file) {
            console.log(file)
          };
        }
        request.execute(callback);
      }
    }
    

    对于 v3 中的示例 javascript 代码,请查看link

    【讨论】:

    • 请更新您的最后一个链接,它链接到 googles drive api 的 javascript 快速入门指南,并没有解释如何使用更新功能。
    猜你喜欢
    • 2012-08-10
    • 2023-03-27
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 2016-06-22
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    相关资源
    最近更新 更多