尝试将文件上传到 google drive api 时收到 404,怀疑是数据格式问题

las*_*nda 2 javascript post request google-drive-api

我试图使用 JS 将一些动态生成的数据(json)保存到我的谷歌驱动器上的(新)文件中,但我在我的 POST 中不断收到这些无用的错误。

起初我以为我发布到错误的地址,但是在更改数据内容后,错误从 404 更改为 400,所以现在我怀疑错误与格式有关(因为我不太了解这些多部分内容)。

代码是这样的:

function gDriveSaveProject(data, gDriveFolderID, currentProj, callback )
    {
    const boundary = '-------314159265358979323846';
    const delimiter = "\r\n--" + boundary + "\r\n";
    const close_delim = "\r\n--" + boundary + "--";

    var metadata = {
    'title': currentProj + ".lo",
    'mimeType': 'application/json',
    'parents' : [{'id' : gDriveFolderID}]
        };

    var multipartRequestBody =
        delimiter +
        'Content-Type: application/json\r\n\r\n' +
        JSON.stringify(metadata) +
        delimiter +
        'Content-Type: application/json\r\n' +
        'Content-Transfer-Encoding: base64\r\n' +
        '\r\n' +
        btoa(JSON.stringify(data)) +
        close_delim;

    var request = gapi.client.request({
        'path': '/upload/drive/v2/files',
        'method': 'POST',
        'params': {'uploadType': 'multipart'},
        'headers': {
            'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
            },
        //'body': ""   // using this gives me 400 instead of 404
        'body': multipartRequestBody
        });

    request.execute(callback);
    }
Run Code Online (Sandbox Code Playgroud)

该代码基于 google drive sdk 示例,但使用动态创建的数据而不是上传的文件和目标目录而不是根目录。

谢谢

las*_*nda 5

弄清楚了!

原来我为父文件夹提供了错误的 ID,这就是为什么我得到 404,但由于它没有作为正确的错误对象返回,我认为 404 是因为 /upload/v2/drive/files 不是成立。

也许谷歌可以让像我这样的白痴更清楚;)