1

I am trying to share upload file using google drive API.I following this tutorial

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?

James Z
  • 12,209
  • 10
  • 24
  • 44
Rigal
  • 611
  • 2
  • 6
  • 25

1 Answers1

2
  • You want to share the files in Google Drive using Drive API with Javascript.
  • You have already been able to get and put for the files on Google Drive using Drive API.

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.

Sample script:

In this sample script, I prepared the function of createPermissions for your situation.

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.
  • At above script, the file is shared with the user, which has the email address of ###, as writer and user. 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.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Hello, Thank you so much for your quick help. It's working fine, but need to change body parameter const body = { role: "writer", type: "user", value: "###" // Please set the email address of user that you want to share. }; emailAddress not supported in body parameter. I replace "emailAddress" with value and it works fine. – Rigal Dec 02 '19 at 16:14
  • Hello, I faced another issue. the receiver must also login with "drive.file scope", and be able to read it currently, the file is shared successfully and gets an email notification and file display in the drive but when we try using API it is not working. – Rigal Dec 02 '19 at 17:12
  • @Rigal Thank you for replying. I'm glad your issue was resolved. About your new issue, from [the official document](https://developers.google.com/drive/api/v3/about-auth), the scope of `drive.file` is `Per-file access to files created or opened by the app.`. So how about trying to use `https://www.googleapis.com/auth/drive`? Although I would like to support you, unfortunately, I'm not sure about your actual situation, so when this didn't resolve your new issue, can you post it as new question by including the detail information? By this, it will help users including me think of your issue. – Tanaike Dec 02 '19 at 22:34
  • I am not able to post a new question. StackOverflow blocks me till 2 days to aks a new question. Would you please upvote the question. Check this Link https://stackoverflow.com/questions/59149984/shared-files-cant-access-using-drive-file-s-google-drive-api Thanks – Rigal Dec 03 '19 at 04:41
  • @Rigal Thank you for replying. Although I'm not sure about your replying, the link of your replying is your question? – Tanaike Dec 03 '19 at 04:58
  • yes my question. Still not find any solution to fix share issues (drive.file)scope. – Rigal Dec 03 '19 at 11:34
  • @Rigal Thank you for replying. Now I noticed that an answer has already been posted. It will resolve your issue. – Tanaike Dec 03 '19 at 22:22
  • No issue is still not resloved. File share is working fine but main issue is " I restricted the Auth scope of my app to per file (https://www.googleapis.com/auth/drive.file) to restrict access to files created or opened by the app. This works fine for my own files created by the app. When I share this file with someone else the file is visible and identified as made by my app but cannot be opened by my app: 'file not found'." If you want i will create git repo so you can check code and help me. Thanks – Rigal Dec 04 '19 at 01:36
  • @Rigal Thank you for replying. I'm not the owner of the answer. And I cannot answer here. I deeply apologize for this. In this case, please comment to the answer for your new question. I think that the owner of answer will answer to your comment and resolve your issue. If you can cooperate to resolve your issue, I'm glad. – Tanaike Dec 04 '19 at 01:48