hoping you can help me figure this one out. Pretty stumped as to why this is giving me issues.
Following the quickstart guide, I'm able to get the files printed to the console. Cool, I see the second argument in files.list
is a callback that's passing in the res/error
Node: v8.3.0 npm: v5.3.0
const { google } = require('googleapis');
const express = require('express');
const fs = require('fs');
const keys = require('./credentials.json');
const app = express();
const drive = google.drive('v3');
const scopes = [
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/drive.metadata.readonly"
];
// Create an oAuth2 client to authorize the API call
const client = new google.auth.OAuth2(
keys.web.client_id,
keys.web.client_secret,
keys.web.redirect_uris[0]
);
// Generate the url that will be used for authorization
this.authorizeUrl = client.generateAuthUrl({
access_type: 'offline',
scope: scopes
});
// Open an http server to accept the oauth callback. In this
// simple example, the only request to our webserver is to
// /oauth2callback?code=<code>
app.get('/oauth2callback', (req, res) => {
const code = req.query.code;
client.getToken(code, (err, tokens) => {
if (err) throw err;
client.credentials = tokens;
// set auth as a global default
google.options({
auth: client
});
listFiles()
res.send('Authentication successful! Please return to the console.');
server.close();
});
});
// This prints out 10 files, all good.
function listFiles() {
drive.files.list({
pageSize: 10,
fields: 'nextPageToken, files(id, name)',
q: "name = 'US'"
}, (err, res) => {
if (err) {
console.error('The API returned an error.');
throw err;
}
const files = res.data.files;
if (files.length === 0) {
console.log('No files found.');
} else {
console.log('Files:');
for (const file of files) {
console.log(`${file.name} (${file.id})`);
}
}
});
}
Specifically, the listFiles
function is not working when I try to use async/await.
Here's what that looks like:
async function listFiles() {
const res = await drive.files.list({
pageSize: 10,
fields: 'nextPageToken, files(id, name)',
q: "name = 'US'"
});
console.log(res);
return res;
}
It's logging undefined
. Any ideas? Thanks!