2

when a shared user is login in App. shared file can't access. API return "no files found'.

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 the shared user gets email notification of share file and when a shared user clicks on a shared file email file is access in google drive. Ie: Manoj (App owner) upload file in google APP(google drive API) and share file with "Rigal". Rigal gets a shared file email notification and file also display(access ) in his google drive. But when Rigal login in App (google drive API) he can't view (access) shared file. Thanks

8
  • Do you encounter the same problem if Manoj shares the files manually with Rigal, without using the API? When the shared user clicks in the email on the link to the file that has been shared with him, is the file / his drive being opened with the same account with which the file has been shared? Or maybe the user has a second account? Commented Dec 4, 2019 at 10:19
  • Hello, @ziganotschka Yes, I have checked manually to share a file with rigal and it is successfully shared After when I log in with API shared file not display using API. Commented Dec 4, 2019 at 11:02
  • So the file is succesfully shared, but does not show up when Rigal tries to access it programmatically with the API? Can you please provide the code that Rigal uses to access the file? Commented Dec 4, 2019 at 11:20
  • Hello, @ziganotschka I following developers.google.com/drive/api/v3/quickstart/js link to get files from google drive. I have change var SCOPES = "googleapis.com/auth/drive.file"; in the above code example. Commented Dec 4, 2019 at 11:30
  • Can the user access the file with different scopes, e.g. https://www.googleapis.com/auth/drive? The documentation developers.google.com/identity/protocols/googlescopes specifies that https://www.googleapis.com/auth/drive.file only allows you to "View and manage Google Drive files and folders that you have opened or created with this app" Commented Dec 4, 2019 at 12:03

1 Answer 1

0

Search results for listing files can be filtered by implementing the search query q

  • If Rigal specifies for q 'sharedWithMe' - only the files will be listed, that have been shared with Rigal by someone else.
  • If Rigal addionally specifies '[email protected]' in owners, this will allow to eliminate from the results the files that have been shared with Rigal by someone else.
  • It is not possible to distinguish between files that have been shared with Rigal by Manoj's App, rather than by Manoj manually, unless Monoj's App makes use of a service account, eventually with impersonation, for sharing the file with Rigal.

A sample based on the Drive v3 API quickstart you use:

function listFiles() {
        gapi.client.drive.files.list({
          'q': "sharedWithMe and '[email protected]' in owners"
          'pageSize': 10,
          'fields': "nextPageToken, files(id, name)"
        }).then(function(response) {
          appendPre('Files:');
          var files = response.result.files;
          if (files && files.length > 0) {
            for (var i = 0; i < files.length; i++) {
              var file = files[i];
              appendPre(file.name + ' (' + file.id + ')');
            }
          } else {
            appendPre('No files found.');
          }
        });
      }
Sign up to request clarification or add additional context in comments.

10 Comments

thanks for your help . Would you please provide a code sample?
I provided a sample based on the quickstart you are using.
Hello @ziganotschka I have followed the below tutorial for upload/access/share files on google drive using google drive API. bytutorial.com/tutorials/google-api/… Would you please suggest how to authenticate the app using a service account? Thanks
This is more tricky - you need to create a service account in the first place - as described here: cloud.google.com/iam/docs/service-accounts and here: cloud.google.com/iam/docs/service-accounts A tutorial how to implement it in JS: dev.to/mornir/… Basically, you need to implement the functions getCredentials() and getDrive() and then replace ` gapi.client.drive.permissions.create` through getDrive().permissions.create
Hello,@ziganotschka I have created a service account and follow steps. I have checked the below tutorial but it's based on Node Js. We need a javascript code directly executed in a browser.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.