当尝试将文件上传到Google drive api时,出现404提示,怀疑是数据格式化问题

getting a 404 when trying to upload a file to the google drive api, suspecting a data formatting issue

本文关键字:提示 出现 怀疑 问题 格式化 数据格式 数据 api 文件 drive Google      更新时间:2023-09-26

我试图使用JS将一些动态生成的数据(json)保存到我的google驱动器上的(新)文件,但我一直在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);
    }

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

谢谢

明白了!

原来,我为父文件夹提供了一个错误的ID,这就是为什么我得到了404,但由于它没有作为一个适当的错误对象返回,我认为404是因为/upload/v2/drive/文件没有找到。

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