【问题标题】:Gapi.client.drive.files.update() Returning error "Parse Error"Gapi.client.drive.files.update() 返回错误“解析错误”
【发布时间】:2019-02-20 16:01:18
【问题描述】:

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

两者都简单地接受一个fileId和两个parentId,一个要删除,一个要使用google api添加。它本质上是对谷歌驱动器中文件的移动操作。有效的代码只需要(from, to, file) => {} 的签名,而没有通过Google Picker 调用回调中的字段的签名。 (Link to Picker API Docs)

第一个返回成功,第二个 get 是来自 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));
        });
    }
}

我需要更多信息来解决这个问题吗?

【问题讨论】:

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


    【解决方案1】:

    工作最终示例

    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 可以正常工作。

    【讨论】:

      猜你喜欢
      • 2015-09-28
      • 1970-01-01
      • 2021-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多