I want the user to have the ability to grant access only to a specific folder in Google Drive. To achieve this, I'm using the drive.files scope and this simple code from the examples.
function createPicker() {
var docsView = new google.picker.DocsView()
.setIncludeFolders(true)
.setSelectFolderEnabled(true)
.setParent('root');
const picker = new google.picker.PickerBuilder()
.enableFeature(google.picker.Feature.NAV_HIDDEN)
.enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
.setDeveloperKey(API_KEY)
.setAppId(APP_ID)
.setOAuthToken(accessToken)
.addView(docsView)
.addView(new google.picker.DocsUploadView())
.setCallback(pickerCallback)
.build();
picker.setVisible(true);
}
async function pickerCallback(data) {
if (data.action === google.picker.Action.PICKED) {
let text = `Picker response: ${JSON.stringify(data, null, 2)}`;
const document = data[google.picker.Response.DOCUMENTS][0];
const fileId = document[google.picker.Document.ID];
console.log(fileId);
const res = await gapi.client.drive.files.get({
'fileId': fileId,
'fields': '*',
});
text += `Drive API response for the first document: ${JSON.stringify(res.result, null, 2)}`;
window.document.getElementById('content').innerText = text;
}
}
In the Google Picker, I select a folder.
I retrieve the folder ID, but when I try to retrieve the list of files, I get an empty result.
$service = new \Google_Service_Drive($this->googleClient);
$folderId = 'id';
$params = array(
'q' => "'$folderId' in parents"
);
$files = $service->files->listFiles($params);
foreach ($files as $file) {
printf("Found file: %s (%s)\n", $file->getName(), $file->getId());
}
If I select a file within the folder and then request the list of files again, I only get that file.
Therefore, my question is: Is it possible to use the Google Picker to obtain access to only a specific folder from the user? If not, are there any alternative options besides using the "https://www.googleapis.com/auth/drive" scope?