【问题标题】:Shared files can't access(get list of shared file) using drive.file scope Google Drive API使用 drive.file 范围 Google Drive API 无法访问共享文件(获取共享文件列表)
【发布时间】:2019-12-03 03:57:19
【问题描述】:

文件共享成功,共享用户收到电子邮件通知,文件显示在用户谷歌驱动器中,但是当我们尝试使用 API 获取共享文件时,它不起作用。

var SCOPES = ["https://www.googleapis.com/auth/drive.file", "profile"];

function createPermissions(fileId, body) {
  gapi.client.load("drive", "v3", function() {
    gapi.client.drive.permissions
      .create({
        fileId: fileId,
        resource: body
      })
      .then(function(res) {
        //console.log(res);
        Swal.fire("Success!", "File has been success shared!", "success");
        // do something
      })
      .catch(function(err) {
        //console.log(err);
        Swal.fire({
          icon: "error",
          title: "Oops...",
          text: "Something went wrong! Plese try agian later!!",
          footer: ""
        });
        // do something
      });
  });
}

以上代码运行正常,文件共享成功,但共享用户登录应用内用户无法访问共享文件。

任何人请建议/帮助解决上述问题?

谢谢

【问题讨论】:

    标签: javascript google-drive-api


    【解决方案1】:

    我建议你以这种方式调用 Drive API:

    // This is a good scope for testing
    const SCOPES = ["https://www.googleapis.com/auth/drive"];
    
    // This code takes into consideration that you already did all the OAuth2.0 proccess 
    // correctly to connect to the Drive API
    module.exports.init =  async function (){
        // Create the Drive service
        const drive = google.drive({version: 'v3', oauth2Client});
        // Create permissions for an user
       await  createPermissions(drive, null, null);
    } 
    
    // This function will create the permissions to share a file using Promises 
    function createPermissions(drive, fileId, body){
        // These parameters are for test, past the values you want as arguments
        fileId = fileId || "your-file-id"; 
        body = body || {
            "role": "writer",
            "type": "user",
            "emailAddress": "user@domain"
        };
        // Create the promise and return the value from the promise if you want to
        return new Promise((resolve, reject) => {
            try {
                // Call the endpoint, if there are no errors you will pass the results to resolve 
                // to return them as the value from your promise
                return drive.permissions.create({
                    fileId: fileId,
                    resource: body
                },
                (err, results) => {
                    if(err) reject(`Drive error: ${err.message}`);
                    resolve(results);
                });
            } catch (error) {
                console.log(`There was a problem in the promise: ${error}`);
            }
        });
    }
    

    我试过了,文件已成功共享给我想要的用户。请记住将您的身体塑造成:

    {
       "role": "writer",
       "type": "user",
       "emailAddress": "user@domain"
    };
    

    文档

    这里有一些链接可以了解有关 Drive API 权限的更多信息:

    【讨论】:

    • 文件共享工作正常,但主要问题是“我将应用程序的身份验证范围限制为每个文件 (googleapis.com/auth/drive.file) 以限制对创建或打开的文件的访问由应用程序。这适用于我自己由应用程序创建的文件。当我与其他人共享此文件时,该文件可见并被识别为由我的应用程序制作,但我的应用程序无法打开:'找不到文件'。谢谢
    • “文件可见并识别为由我的应用程序制作但无法由我的应用程序打开”是什么意思?这就像说 John Doe 制作并且他是文件的所有者,但他无法打开它。你检查过service accounts吗?
    • 您好,@alberto-vielma 当共享用户登录 App 时。共享文件无法访问。 API返回“未找到文件”。共享用户收到共享文件的电子邮件通知,当共享用户单击共享文件时,电子邮件文件可在谷歌驱动器中访问。即:Manoj(应用程序所有者)在谷歌APP(谷歌驱动器)中上传文件API)并与“Rigal”共享文件。Rigal 收到共享文件电子邮件通知,文件也显示(访问)在他的谷歌驱动器中。但是当 Rigal 登录应用程序(谷歌驱动器 API)时,他无法查看(访问)共享文件. 谢谢
    • 好的,我现在好了。当“userA”与“userB”共享文件时,该文件将不会保存到 userB 的驱动器中。在这种情况下,您有两个选择:从原始文件创建副本或将所有权从用户 A 转移给用户 B。
    • 不,文件保存在用户 b 驱动器中,但问题是当用户 b 登录应用程序(谷歌驱动器 API)时用户 b 无法使用 API 访问文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多