共享文件无法使用drive.file范围Google Drive API访问(获取共享文件列表) [英] Shared files can't access(get list of shared file) using drive.file scope Google Drive API

查看:235
本文介绍了共享文件无法使用drive.file范围Google Drive API访问(获取共享文件列表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The file is shared success and the shared user gets an email notification, file display in the user google drive but when we try using API to get shared files, it is not working.

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
      });
  });
}

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

The above code is working fine, the file is successfully shared but when shared user login in-app user can't access shared files.

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

Anyone please suggest/help to fix the above issue?

谢谢

推荐答案

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

I would suggest you call the Drive API in this way:

// 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}`);
        }
    });
}

我尝试过,文件成功共享给我想要的用户.请记住,以如下方式构建您的身体:

I tried it and the files are shared successfully to the user I wanted. Keep in mind to build your body as:

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

文档

以下是一些链接,可进一步了解Drive API权限:

Docs

Here are some links to know more about the Drive API Permissions:

权限:创建.

Permissions: create.

这篇关于共享文件无法使用drive.file范围Google Drive API访问(获取共享文件列表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆