首页 > 解决方案 > Google Drive API 在可恢复上传时返回意外结果

问题描述

我正在尝试遵循Google 的Google Drive API 交互文档,特别是执行可恢复上传。我希望能够上传更大的 50MB+ 图像文件,以及修改上传文件在 Google Drive 中的描述以包含一些用户输入的文本。

我能够毫无问题地执行登录/身份验证。/token我对 OAuth2端点运行 jQuery AJAX 调用,它为我提供了一个有效的access_token,然后我可以使用它来直接调用 API。

然后,我根据文档对端点进行 AJAX 调用/files?uploadType=resumable,将 HTTP 标头设置为包含access_token,设置Content-Type,等等。我还包括元数据,在这种情况下,它是新上传的图像文件的预期名称。

但是,我从 API 得到的响应令人困惑。它给了我一个status200,根据文档,这是预期的响应,但返回的statusText读数为"parsererror".

根据 Google 文档,我应该收到location一串可恢复会话 URI,然后我可以将其用作 PUT 请求的端点,其中我将文件的数据包含在请求正文中。但是,在我收到的对象中,我没有看到任何location预期的字符串或 URI。

任何帮助将不胜感激。这是返回令人困惑的对象的 jQuery AJAX 调用:

function fileUpload() {

        var metadata = { "name" : "New Image" };

        $.ajax({
            type: "POST",
            beforeSend: function(request) {
                request.setRequestHeader("Authorization", "Bearer" + " " + localStorage.getItem("accessToken"));
                request.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
                request.setRequestHeader("X-Upload-Content-Type", new_mime_type);
                request.setRequestHeader("X-Upload-Content-Length", new_file_size);          
            },
            cache: false,
            dataType: "json",
            data: JSON.stringify(metadata),
            url: "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable",
            success: function(data) {
                console.log("Resumable upload POST call success. Returned data:");
                console.log(data);

                // Do a second $.ajax POST call here...?

            },
            error: function(data) {
                console.log("Resumable upload POST call error. Returned data:");
                console.log(data);
            }
        });

    }

标签: jqueryajaxapigoogle-drive-apidrive

解决方案


推荐阅读