List publicly shared files from google drive using

2019-08-15 20:28发布

问题:

I have been facing problem while coding that while using service account key and using drive.files.list API, i am not able to get files, though i have shared all the files publicly

const { google } = require('googleapis');
const drive = google.drive('v3');
const jwtClient = new google.auth.JWT(
    client_email,
    null,
    private_key,
    ['https://www.googleapis.com/auth/drive']
);

jwtClient.authorize(function (err, tokens) {
    if (err) {
        console.log(err);
        return;
    } else {
        console.log("Successfully connected!");
    }
});


// List Drive files.
drive.files.list({
    auth: jwtClient,
    // fields: "files",
    includeRemoved: false,
    spaces: 'drive'
}, (listErr, resp) => {
    if (listErr) {
        console.log(listErr);
        return;
    }
    console.log("Result ========>", resp.data.files)
});
  • In result i am getting nothing though i have shared all the files publicly and given edit rights to all.
  • In service account i have enable google API and gave qwner permission.
  • I want all the publicly share files to list.
  • Where i am wrong in the code.

please help me out in this. thanks :)

回答1:

A service account is not you it is a dummy user. As like any user a files.list will only return the files that it has access to. That being files it has created and files someone has shared with it. While one would think that public files everyone has access to. If a files.list returned a list of all the files on Google drive that were set to public that would be a huge list and not very useful.

By setting a file to public if you have the file id you will be able to see it using an API key. You wont be able to edit it but you will be able to do a file.get on it.

Take the service account email address and go to google drive share the folder that contains the files with the service account like you would any other user. You can also share each file individually with the service account if you wish. Then try and do a files.list.