Reputation: 171
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)
});
please help me out in this. thanks :)
Upvotes: 2
Views: 2923
Reputation: 101
Sharing a domain-wide Shared Folder with the service account email did not work for me.
But, I did find this article
(https://nilsnh.no/2018/12/17/how-to-implement-domain-wide-delegation-in-gsuite-using-node.js)
that explained that you can pass an "email address to impersonate" to the subject parameter of the google.auth.JWT call. It is now as if you logged in as that user. You should probably create a specific domain user for this purpose. This dedicated user will not have any personal drive files, but it will allow you to access the domain-wide shared drives without having to use that user's password.
const emailAddressToImpersonate = '[email protected]'
const jwtClient = new google.auth.JWT(
client_email,
null,
private_key,
['https://www.googleapis.com/auth/drive'],
emailAddressToImpersonate
);
Upvotes: 0
Reputation: 117271
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.
Upvotes: 3