node.js - 具有 2 条腿的 oauth 的 Google Drive file.update 返回正常,但无法按预期工作

标签 node.js google-drive-api

我在 console.developers.google.com 中创建了一个服务帐户,并且已将所述服务的客户端 ID 添加到 admin.google.com 中,以便在“高级设置”>“身份验证”>“管理 OAuth 客户端访问”下进行全域委派。

使用Google Nodejs api clientGoogle Nodejs auth library ,然后我使用以下方法创建了一个 jwt 客户端:

const jwtClient = new google.auth.JWT(
        key.client_email,
        null,
        key.private_key,
        [
            'https://www.googleapis.com/auth/spreadsheets',
            'https://www.googleapis.com/auth/drive',
            'https://www.googleapis.com/auth/drive.file',
            'https://www.googleapis.com/auth/drive.appdata',
            'https://www.googleapis.com/auth/drive.scripts',
            'https://www.googleapis.com/auth/drive.metadata'
        ],
        'user@domain.com'
);

并与

const google = require('googleapis');
const sheets = google.sheets('v4');
const drive = google.drive('v3');

我跑了

async.waterfall(
    [
        (callback) => {
            sheets.spreadsheets.create(
                {
                    auth: jwtClient,
                    resource: {
                        properties: {
                            title: 'Testing'
                        }
                    }
                },
                (err, spreadsheet) => {
                    console.log(spreadsheet);
                    callback(err, spreadsheet.spreadsheetId)
                }
            )
        },
        (spreadsheetId, callback) => {
            drive.files.update(
                {
                    auth: jwtClient,
                    fileId: spreadsheetId,
                    resource: {
                        addParents: config.folder
                    }
                },
                (err, file) => {
                    console.log(file);
                    callback(err, file);
                }
            )
        }
    ],
    (err, results) => {
        if (err) {
            console.log(err);
            res.status(400).send({ error: err.toString() });
        } else {
            res.status(200).send("It lives!");
        }
    }
);

我对 file.update 的第二次调用的响应返回 200 和文件的响应:

{ kind: 'drive#file',
  id: '<id-here>',
  name: 'Testing',
  mimeType: 'application/vnd.google-apps.spreadsheet' }

但奇怪的是......addParents 没有反射(reflect)在云端硬盘网络应用程序上的文件详细信息中。

任何想法。我正在与 Google api 支持联系,但在大约一周内没有任何进展。非常感谢您的帮助!

最佳答案

问题在于 addParents 是一个查询参数,而不是文件资源中的字段。因此,应将其指定为 fileId 的对等点:

drive.files.update(
    {
        auth: jwtClient,
        fileId: spreadsheetId,
        addParents: config.folder
    },
    ...

关于node.js - 具有 2 条腿的 oauth 的 Google Drive file.update 返回正常,但无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38590881/

相关文章:

node.js - Azure函数上下文日志未正确记录,不同步

node.js - 了解 NodeJS 中 require 的行为

javascript - 无法安装任何 NPM 包,错误 4058

node.js - AWS Elastic Beanstalk 中的 pm2 重启循环( Node 环境)

google-chrome-extension - Google Drive 前端 UI 事件监听器

version-control - 通过 Crouton 在 Chromebook 上本地访问 Google Drive

javascript - 加载经过训练的模型并在 Node js 中运行测试

javascript - 运行 do while 循环会导致 fatal error

javascript - 如何从 Google Picker JavaScript API 获取直接下载 URL?

ssh - 如何通过 SSH/SCP 将文件发送到 Google Drive?