The title says it all, fields
and q
don't work for some reason.
In my Drive, I have a folder called "Imprensa", which fatherId
corresponds to, inside this folder I have more folders, these beign used as category per folder (e.g.: news, article, informative, etc).
The query is suposed to get these folders, folders that have as parent the "father" (Imprensa), but instead it brings up everything that is contained within my Drive.
I just copied the quickstart from Google Drive APIs Rest docs for node, so basicly, the code is the same from there.
Drive API v3
googleapis v26.0.1
var fs = require('fs');
var readline = require('readline');
var google = require('googleapis');
var googleAuth = require('google-auth-library');
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/drive-nodejs-quickstart.json
var SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly'];
var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
process.env.USERPROFILE) + '/.credentials/';
var TOKEN_PATH = TOKEN_DIR + 'drive-nodejs-quickstart.json';
// Load client secrets from a local file.
fs.readFile('client_secret.json', function processClientSecrets(err, content) {
if (err) {
console.log('Error loading client secret file: ' + err);
return;
}
// Authorize a client with the loaded credentials, then call the
// Drive API.
authorize(JSON.parse(content), listFiles);
});
function authorize(credentials, callback) {
var clientSecret = credentials.installed.client_secret;
var clientId = credentials.installed.client_id;
var redirectUrl = credentials.installed.redirect_uris[0];
var auth = new googleAuth();
var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, function (err, token) {
if (err) {
getNewToken(oauth2Client, callback);
} else {
try {
oauth2Client.credentials = JSON.parse(token);
} catch (error) {
console.log('error: ', error);
}
callback(oauth2Client);
}
});
}
function getNewToken(oauth2Client, callback) {
var authUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
});
console.log('Authorize this app by visiting this url: ', authUrl);
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter the code from that page here: ', function (code) {
rl.close();
oauth2Client.getToken(code, function (err, token) {
if (err) {
console.log('Error while trying to retrieve access token', err);
return;
}
oauth2Client.credentials = token;
storeToken(token);
callback(oauth2Client);
});
});
}
function storeToken(token) {
try {
fs.mkdirSync(TOKEN_DIR);
} catch (err) {
if (err.code != 'EEXIST') {
throw err;
}
}
fs.writeFile(TOKEN_PATH, JSON.stringify(token));
console.log('Token stored to ' + TOKEN_PATH);
}
function listFiles(auth) {
var service = google.google.drive('v3');
const fatherId = '15buNwg19v6u9tY4AuJBjmy1q1ySdiRo4';
service.files.list({
auth: auth,
// 'orderBy': 'modifiedTime desc',
fields: 'nextPageToken, files(id, name)',
q: `'${fatherId}' in parents`
}, function (err, response) {
var folders = response.files;
console.log('folders: ', folders);
});
}
How about this modification?
q: `parents in '${fatherId}'`
q: `'${fatherId}' in parents`
If this was not useful for your situation, I'm sorry.
It was found that the reason of this issue was due to the version of googleapis. When v26 is used, the issue occurs. When googleapis is downgraded to v24, no issue occurs.