驱动服务 API 一些参数不工作
Drive service API some parameters not working
标题说明了一切,fields
和 q
出于某种原因不起作用。
在我的云端硬盘中,我有一个名为 "Imprensa" 的文件夹,与 fatherId
对应,在这个文件夹中我有更多文件夹,这些被用作每个文件夹的类别(例如:新闻、文章、信息等) .
该查询应该获取这些文件夹,这些文件夹具有 parent 和 "father" (Imprensa),但它会显示我的驱动器中包含的所有内容。
我只是从 Google Drive APIs Rest docs for node 复制了快速入门,所以基本上,代码与那里相同。
驱动器 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);
});
}
这个修改怎么样?
发件人:
q: `parents in '${fatherId}'`
收件人:
q: `'${fatherId}' in parents`
注:
- 这假定您已经可以检索访问令牌。
参考:
如果这对您的情况没有用,我很抱歉。
编辑:
发现这个问题的原因是googleapis的版本问题。使用 v26 时,会出现此问题。当 googleapis 降级到 v24 时,不会出现任何问题。
标题说明了一切,fields
和 q
出于某种原因不起作用。
在我的云端硬盘中,我有一个名为 "Imprensa" 的文件夹,与 fatherId
对应,在这个文件夹中我有更多文件夹,这些被用作每个文件夹的类别(例如:新闻、文章、信息等) .
该查询应该获取这些文件夹,这些文件夹具有 parent 和 "father" (Imprensa),但它会显示我的驱动器中包含的所有内容。
我只是从 Google Drive APIs Rest docs for node 复制了快速入门,所以基本上,代码与那里相同。
驱动器 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);
});
}
这个修改怎么样?
发件人:
q: `parents in '${fatherId}'`
收件人:
q: `'${fatherId}' in parents`
注:
- 这假定您已经可以检索访问令牌。
参考:
如果这对您的情况没有用,我很抱歉。
编辑:
发现这个问题的原因是googleapis的版本问题。使用 v26 时,会出现此问题。当 googleapis 降级到 v24 时,不会出现任何问题。