[英]Self downloading an xlsx file from google Driver
因此,我正在嘗試制作一個小腳本,該腳本將使用谷歌驅動器 API 下載一個肯定的 excel 文件,通過遵循谷歌 API 教程,我遇到了兩個錯誤“無法讀取未定義的屬性'on'”和“請求的轉換是不支持”這里是代碼:
const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');
const SCOPES = ['https://www.googleapis.com/auth/drive'];
const TOKEN_PATH = 'token.json';
fs.readFile('credentials.json', (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
authorize(JSON.parse(content), listFiles);
});
/**
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
function authorize(credentials, callback) {
const {client_secret, client_id, redirect_uris} = credentials.installed;
const oAuth2Client = new google.auth.OAuth2(
client_id, client_secret, redirect_uris[0]);
fs.readFile(TOKEN_PATH, (err, token) => {
if (err) return getAccessToken(oAuth2Client, callback);
oAuth2Client.setCredentials(JSON.parse(token));
callback(oAuth2Client);
});
}
/**
* @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for.
* @param {getEventsCallback} callback The callback for the authorized client.
*/
function getAccessToken(oAuth2Client, callback) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
console.log('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter the code from that page here: ', (code) => {
rl.close();
oAuth2Client.getToken(code, (err, token) => {
if (err) return console.error('Error retrieving access token', err);
oAuth2Client.setCredentials(token);
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
if (err) return console.error(err);
console.log('Token stored to', TOKEN_PATH);
});
callback(oAuth2Client);
});
});
}
/**
* Lists the names and IDs of up to 10 files.
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
function listFiles(auth) {
const drive = google.drive({version: 'v3', auth});
drive.files.list({
pageSize: 10,
fields: 'nextPageToken, files(id, name)',
}, (err, res) => {
if (err) return console.log('The API returned an error: ' + err);
const files = res.data.files;
if (files.length) {
console.log('Files:');
files.map((file) => {
console.log(`${file.name} (${file.id})`);
});
} else {
console.log('No files found.');
}
var fileId = '1lKhyW1O519_1V1QhL9Vkbu55HqyfrgaUbnF4fmhZqU0';
var dest = fs.createWriteStream('/home/oem/Desktop/TTHIS/report-2019-03-26.xls');
drive.files.export({
fileId: fileId,
mimeType: 'xls'
})
.on('end', function () {
console.log('Done');
})
.on('error', function (err) {
console.log('Error during download', err);
})
.pipe(dest);
});
}
記住代碼的第一部分只是為了授權,所以真正的問題從函數 listFiles() 開始。 感謝您的時間!
這個改裝怎么樣?
response.data
,使用{responseType: 'stream'}
。
xls
mimeType 。 對於 xlsx 格式,請使用application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
的 mimeType。請修改如下。
從: var fileId = '1lKhyW1O519_1V1QhL9Vkbu55HqyfrgaUbnF4fmhZqU0'; var dest = fs.createWriteStream('/home/oem/Desktop/TTHIS/report-2019-03-26.xls'); drive.files.export({ fileId: fileId, mimeType: 'xls' }) .on('end', function () { console.log('Done'); }) .on('error', function (err) { console.log('Error during download', err); }) .pipe(dest);
到:
var fileId = '1lKhyW1O519_1V1QhL9Vkbu55HqyfrgaUbnF4fmhZqU0'; var dest = fs.createWriteStream('/home/oem/Desktop/TTHIS/report-2019-03-26.xls'); drive.files.export({ fileId: fileId, mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', }, {responseType: 'stream'}, function(err, response) { response.data .on('end', function() { console.log("Done."); }) .on('error', function(err) { console.log('Error during download', err); return process.exit(); }) .pipe(dest); });
1lKhyW1O519_1V1QhL9Vkbu55HqyfrgaUbnF4fmhZqU0
的文件是電子表格。在我的環境中,我可以確認這個修改后的腳本有效。 但是,如果這對您的環境不起作用,我深表歉意。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.