繁体   English   中英

共享文件无法访问(获取共享文件列表)使用 drive.file 范围 Google Drive API

[英]Shared files can't access(get list of shared file) using drive.file scope Google Drive API

文件共享成功,共享用户收到电子邮件通知,文件显示在用户谷歌驱动器中,但是当我们尝试使用 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
      });
  });
}

上面的代码工作正常,文件已成功共享,但当共享用户登录时,应用内用户无法访问共享文件。

有人请建议/帮助解决上述问题吗?

谢谢

我建议您以这种方式调用 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 权限的更多信息:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM