Gapi.client.drive.files.update()返回错误“解析错误”

时间:2019-02-20 16:01:18

标签: javascript google-api google-drive-api google-picker

使用google api,我必须设置本质上相同但产生不同结果的代码集。

两者都只需输入一个fileId和两个parentId,一个就可以删除,一个要与Google api一起添加。本质上,这是Google驱动器中文件的移动操作。可以正常工作的代码仅需要(from, to, file) => {}的签名,而没有传递给Google Picker Call回调中的字段的签名。 (Link to Picker API Docs

第一个返回成功,第二个得到来自api的响应,指出存在解析错误,关于导致解析错误的原因很少。

示例:

工作示例

moveFile = (from, to, file) => {
    return gapi.client.drive.files
        .update({
            addParents: to,
            removeParents: from,
            fileId: file,
        })
        .then((res) => {
            logger.log(funcname, `Moved File: ${file} from ${from} to ${to}.`);
            return res.result;
        });
};

非工作示例

function folderSelectedCallback(folderData, fileData) {

    //folderData and fileData both have the same structure
    //{action: 'type of action', 
    //docs: [{Array Of Document Objects}],
    //viewToken: [Array of unused data]}

    //Docs Array Objects Structure (only used properties listed.)
    //{ id: 'string ID of the File',
    //parentId: 'string ID of the parent folder'}

    if (folderData.action === 'picked') {
        console.log('folderSelectedCallback(): called.');
        console.log('File Data: ', fileData);
        console.log('Folder Data: ', folderData);
        const files = fileData.docs;
        const folder = folderData.docs[0];

        console.log(files);
        console.log(folder);

        files.forEach((f) => {
            gapi.client.drive.files
                .update({
                    addParents: to,
                    removeParents: from,
                    fileId: file,
                })
                .then((res) => {
                    console.log(funcname, `Moved File: ${file} from ${from} to ${to}.`);
                    return res.result;
                }).catch(err => console.error(err));
        });
    }
}

我需要解决更多信息吗?

1 个答案:

答案 0 :(得分:0)

最终示例

function folderSelectedCallback(folderData, fileData) {
    if (folderData.action === 'picked') {
        console.log('folderSelectedCallback(): called.');
        console.log('File Data: ', fileData);
        console.log('Folder Data: ', folderData);
        const files = fileData.docs;
        const folder = folderData.docs[0];

        console.log(files);
        console.log(folder);

        files.forEach((f) => {

            const options = {
                addParents: folder.id,
                removeParents: (typeof f.parentId !== 'undefined') ? f.parentId : '',
                fileId: f.id
            };

            gapi.client
                .request({
                    path: `drive/v3/files/${options.fileId}`,
                    method: 'PATCH',
                    params: options,
                    body: options
                })
                .then((response) => console.log(response))
                .catch((err) => console.error(err));
        });
    }
}

出于某种原因,以这种方式使用gapi.client.request可以正常工作。