【问题标题】:Google Drive API: How to create a file in appDataFolder?Google Drive API:如何在 appDataFolder 中创建文件?
【发布时间】:2021-05-07 03:29:42
【问题描述】:

我正在阅读此文档: https://developers.google.com/drive/api/v3/appdata

这是我的代码:

var fileMetadata = {
    'name': 'config.json',
    'parents': ['appDataFolder']
};
var media = {
    mimeType: 'application/json',
    body: '"sample text"'
};
const request = gapi.client.drive.files.create({
    resource: fileMetadata,
    media,
    fields: 'id'
})
request.execute(function (err, file) {
    if (err) {
        // Handle error
        console.error(err);
    } else {
        console.log('Folder Id:', file.id);
    }
})

我收到 403 错误:“用户对此文件没有足够的权限。”

用户没有权限在他的 appDataFolder 中创建文件吗?如何在其中创建文件?

gapi 客户端的范围是 'https://www.googleapis.com/auth/drive.appdata' 并且用户接受了它。

【问题讨论】:

    标签: javascript google-api


    【解决方案1】:

    我相信这个错误的原因是你只使用范围访问 appdata 文件夹,而不是创建文件的范围。访问应用程序数据文件夹和创建文件是两件不同的事情。根据您的代码,您正在尝试在 appdata 文件夹中创建一个文件。

    我建议你同时包含这两个范围:

    https://www.googleapis.com/auth/drive.appdata
    https://www.googleapis.com/auth/drive.file
    

    如果您没有使用增量授权,请确保撤销访问并重新授权。

    参考:https://developers.google.com/drive/api/v3/about-auth#OAuth2Authorizing

    【讨论】:

      【解决方案2】:

      您实际上不需要https://www.googleapis.com/auth/drive.file 范围来创建或删除appDataFolder 内的数据。 https://www.googleapis.com/auth/drive.appdata 范围涵盖了所有这些。

      试试这个。只需将您的身份验证客户端传递给createFile() 函数即可。

      // Requiring the modular service is much better than requiring the whole GAPI
      const GDrive = require('@googleapis/drive');
      
      function createFile(auth) {
          const drive = GDrive.drive({version: 'v3', auth});
      
          const fileMetadata = {
              'name': 'config.json',
              'parents': ['appDataFolder']
          };
          const media = {
              mimeType: 'application/json',
              body: '{"TEST": "THIS WORKED"}'
          };
      
          drive.files.create({
              resource: fileMetadata,
              media: media,
              fields: 'id'
          }).then((resp) => {
              console.log('File Id: ', resp.data.id);
          }).catch((error) => {
              console.error('Unable to create the file: ', error);
          });
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-03
        • 1970-01-01
        相关资源
        最近更新 更多