简体   繁体   English

使用 Google Drive API 与特定用户共享上传的文件

[英]Share uploaded files with specific user using google Drive API

I am trying to share upload file using google drive API.I following this tutorial我正在尝试使用谷歌驱动器 API 共享上传文件。我遵循教程

Upload files/folder, List drive file are working fine but I want to add share functionality where file can we share with specific user.上传文件/文件夹,列出驱动器文件工作正常,但我想添加共享功能,我们可以在其中与特定用户共享文件。

I didn't found any solution.我没有找到任何解决方案。 how to share file?如何共享文件?

  • You want to share the files in Google Drive using Drive API with Javascript.您想使用带有 Javascript 的 Drive API 共享 Google Drive 中的文件。
  • You have already been able to get and put for the files on Google Drive using Drive API.您已经能够使用 Drive API 获取和放置 Google Drive 上的文件。

If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样? Please think of this as just one of several answers.请将此视为几个答案之一。

In order to give the permissions for the file, the method of "Permissions: create" in Drive API is used.为了给文件赋予权限,使用Drive API中的“权限:创建”方法。

Sample script:示例脚本:

In this sample script, I prepared the function of createPermissions for your situation.在这个示例脚本中,我为您的情况准备了createPermissions函数。

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)

      // do something

    })
    .catch(function(err) {
      console.log(err)

      // do something

    });
  });
}

When you use this function, please use as follows.使用此功能时,请按如下方式使用。

const fileId = "###";  // Please set the file ID.
const body = {
  role: "writer",
  type: "user",
  emailAddress: "###"  // Please set the email address of user that you want to share.
};
createPermissions(fileId, body);
  • When you use this script, please prepare the file ID of the file you want to share.使用此脚本时,请准备好要共享的文件的文件ID。
  • At above script, the file is shared with the user, which has the email address of ### , as writer and user .在上面的脚本中,文件与用户共享,用户的电子邮件地址为### ,作为writeruser About the detail information of parameters, please check the official document .关于参数的详细信息,请查看官方文档

Note:笔记:

  • Unfortunately, in your actual situation, I'm not sure where you want to use the above sample script in your script.不幸的是,在您的实际情况中,我不确定您想在脚本中的何处使用上述示例脚本。 So please add it to your script.因此,请将其添加到您的脚本中。

Reference:参考:

If I misunderstood your question and this was not the direction you want, I apologize.如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

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

相关问题 如何将用户上传的文件定向到特定的 Google Drive 文件夹 - How to direct user-uploaded files to a specific Google Drive folder 使用 Google Drive REST API 的网络应用程序如何与使用同一应用程序的其他用户共享文件? - How can a webapp using the Google Drive REST API share files with another user using the same app? 列出当前用户可以使用Google Drive API编辑的文件 - List files current user can edit using Google Drive API 在谷歌驱动器中找不到上传的文件 - Uploaded files not found in google drive 共享文件用户无法在 Google Drive API 中使用 drive.file 范围访问文件 - Shared file user can't access files using drive.file scope In google drive API 让上传的文件登陆特定文件夹并获取上传文件的 url(全部在谷歌驱动器中) - have the uploaded files landing in a specific folder and getting the url of the uploaded file (all in google drive) 无法使用API​​在Google云端硬盘中打开上传的文件 - Can't open uploaded file in Google Drive using API 使用Google Drive SDK共享文件 - Share files with Google Drive SDK 使用Google Drive API上传大文件 - Using Google Drive API to upload large files 使用API​​对元数据进行迭代的Google Drive文件 - Iterating Google Drive files using API for metadata
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM