无法使用node.js和OAuth令牌在Google云端硬盘中创建文件

时间:2017-11-13 23:58:09

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

我看过的所有内容都表明我的代码应该可以运行,而Firebase函数控制台中的日志会显示它返回 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云端硬盘中。当我运行包含示例中其余代码的代码时,电子表格仍会更新。日志显示正在调用这些功能,但Google云端硬盘中没有任何操作。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

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