1

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?

Thanks

Rigal
  • 611
  • 2
  • 6
  • 25
Test
  • 31
  • 1
  • 5

1 Answers1

0

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

Docs

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

Community
  • 1
  • 1
alberto vielma
  • 2,302
  • 2
  • 8
  • 15
  • The file share is working fine but the main issue is " I restricted the Auth scope of my app to per file (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'." Thanks – Rigal Dec 04 '19 at 03:26
  • What do you mean by "the file is visible and identified as made by my app but cannot be opened by my app"? It's like saying John Doe made and he's the file's owner, but he can't open it. Have you checked about [service accounts](https://cloud.google.com/compute/docs/access/service-accounts)? – alberto vielma Dec 04 '19 at 08:13
  • Hello, @alberto-vielma when a shared user is a login in App. shared file can't access. API return "no files found'. 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 – Rigal Dec 04 '19 at 08:30
  • Ok, I get it better now. When a "userA" shares a file to "userB", that file will not be saved into userB's Drive. You have two options in that case: Create a copy from the original file or transfer ownership from userA to userB. – alberto vielma Dec 04 '19 at 13:13
  • Nope, file save in User b drive, but the issue is when userb login in app (google drive API) userb can't access file using API. – Rigal Dec 05 '19 at 02:58
  • Check [this](https://stackoverflow.com/questions/22403014/google-drive-api-scope-and-file-access-drive-vs-drive-files), if you want that Scope I would recommend you to use [service accounts](https://cloud.google.com/compute/docs/access/service-accounts), because the scope makes that your app it is only one to have access to the files, not the users per se – alberto vielma Dec 05 '19 at 08:47