驱动服务API一些参数不起作用

时间:2018-02-23 21:04:28

标签: node.js google-api-nodejs-client

标题说明了一切,fieldsq因某些原因无效。 在我的云端硬盘中,我有一个名为“Imprensa”的文件夹,fatherId对应,在此文件夹中我有更多文件夹,这些格式用作每个文件夹的类别(例如:新闻,文章,信息等)。 该查询用于获取这些文件夹,文件夹具有父亲“父亲”(Imprensa),但它会显示我的驱动器中包含的所有内容。 我刚从Google Drive APIs Rest docs for node复制了快速入门,所以基本上,代码是相同的。
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);        
    });
}

1 个答案:

答案 0 :(得分:0)

这次修改怎么样?

来自:

q: `parents in '${fatherId}'`

致:

q: `'${fatherId}' in parents`

注意:

  • 这假设您已经可以检索访问令牌。

参考:

如果这对你的情况没用,我很抱歉。

编辑:

发现此问题的原因是由于googleapis的版本。使用v26时,会出现问题。当googleapis降级为v24时,不会出现任何问题。