【问题标题】:Unable to create file in Google Drive with node.js and OAuth tokens无法使用 node.js 和 OAuth 令牌在 Google Drive 中创建文件
【发布时间】:2017-11-13 23:58:08
【问题描述】:

我查看的所有内容都表明我的代码应该可以工作,并且 Firebase Functions 控制台中的日志显示它返回 Function execution took 6 ms, finished with status: 'ok' 我的代码基于此示例:https://github.com/firebase/functions-samples/tree/master/google-sheet-sync 我从中获取日志Function execution took 296 ms, finished with status: 'ok'

在我让 google-sheets 示例工作后,我基本上复制/粘贴了它并更改了几行,但无论我做什么,我似乎都无法让 Google Drive 工作。

这是我想出的代码(如果您查看 google-sheets 示例,您会发现它们基本相同)。

exports.makenewfolder = functions.database.ref(`${CONFIG_DATA_PATH}/{ITEM}`).onWrite(
      event => {
        // Since we're not dealing with the data from the DB, don't need the spreadsheets info
        // Just need to return the promise with auth included
        /*var fileMetadata = {
          'name': 'test folder',
          'mimeType': 'application/vnd.google-apps.folder'
        };*/

        var fileMetadata = {
          name: 'Test',
          mimeType: 'text/plain'
        }
        return drivePromise(this.fileMetadata);
      }
    )

    function drivePromise(requestWithoutAuth) {
      return new Promise((resolve, reject) => {
        getAuthorizedClient().then(client => {
          const service = google.drive({ version: 'v3', auth: client });
          const request = requestWithoutAuth;
          request.auth = client;

          service.files.create({
            resource: {
              name: 'Test',
              mimeType: 'text/plain'
            },
            media: {
              mimeType: 'text/plain',
              body: 'Hello World'
            }
          }, (err, file) => {
            if (err) {
              // handle error
              console.error(err);
            } else {
              console.log('Folder id: ', file.id);
            }
          }    
        );
        }).catch(() => reject());
      })
    }

为什么这不起作用?它应该可以工作,因为 SCOPE 是针对https://www.googleapis.com/auth/drive 而不是/auth/spreadsheets,所以它不应该再修改电子表格,除非电子表格位于 Google Drive 中。当我运行包含示例中其余代码的代码时,电子表格仍然会更新。日志显示正在调用这些函数,但 Google Drive 中没有任何反应。我该如何解决这个问题?

【问题讨论】:

    标签: node.js google-drive-api google-cloud-functions


    【解决方案1】:

    return drivePromise(this.fileMetadata); 显然不起作用。 return drivePromise(fileMetadata); 确实如此。叹息。

    【讨论】:

      猜你喜欢
      • 2016-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-05
      • 1970-01-01
      • 1970-01-01
      • 2015-05-30
      • 1970-01-01
      相关资源
      最近更新 更多