Node.js Google云端硬盘api下载文件错误 [英] Node.js Google Drive api Download file error

查看:66
本文介绍了Node.js Google云端硬盘api下载文件错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

const fs = require('fs');
const { google } = require('googleapis');
const Auth = require('../authorization/auth_drive/auth');

// get data
var gotData = function downloadFiles(fileId, callback) {
    let content = fs.readFileSync('./GEARS/authorization/client_secret.json');
    Auth(JSON.parse(content), (auth) => {
        //const returnData = [];
        const drive = google.drive({ version: 'v3', auth });

        const fileId = '';
        const dest = fs.createWriteStream('./test_files/test.png');

        drive.files.get({fileId: fileId, alt: 'media'})
        .on('end', () => {
            console.log('Done');
        })
        .on('error', err => {
            console.log('Error', err);
        })
        .pipe(dest);
    });
};

module.exports = gotData;

遵循Google文档 LINK

Following the google documentation LINK

我尝试过的所有其他功能(例如列表,复制,上载或表格读和写)对我来说都非常有效,除了下载.我的应用程序立即崩溃并显示错误

All other functions I have tried, like List, Copy, Upload or Sheets Read&Write work perfectly for me, except for the Download. My application instantly crashes with the error

TypeError:无法读取未定义的属性"on"

TypeError: Cannot read property 'on' of undefined

我查看了Google的此功能示例,但没有发现任何不同之处,为什么它对Google Developers有效,而对我却无效?

I have looked at Google's Sample of this very functionality, and I have not found anything that differs, so why does it work for Google Developers, while for me it doesn't?

我在Google上没有找到一个遇到此问题的人,所以也许,如果您了解应该怎么做,就可以知道我做错了.

I have not found a single person with this problem on google, so maybe, if you understand what should be done, you can see what I am doing wrong.

-我将包含此链接,这是我拥有的复制功能,在万一出现的情况下效果很好这将是相关的.

-- I will include THIS LINK, that is a Copy function I have, which works perfectly, in case it would be relevant.

谢谢您的建议.

推荐答案

从您的脚本来看,我认为您可能使用的是版本超过v24.0.0的googleapi.那么如何修改googleapis的版本呢?另外,您的情况也遇到了同样的问题.就我而言,该问题是通过将googleapis版本修改为v24.0.0来解决的.

From your script, I thought that you may be using the googleapis of the version more than v24.0.0. So how about modifying the version of googleapis? Also I have experienced the same issue with your situation. In my case, the issue was removed by modifying the version of googleapis to v24.0.0.

当我使用googleapis的v25.0.0-v30.0.0时,发生错误.当它使用v24.0.0的googleapis时,该错误已消除.

When I used v25.0.0 - v30.0.0 of googleapis, the error occurs. When it used the googleapis of v24.0.0, the error was removed.

  • 对于v25.0.0以后版本的googleapi,报告了一些API错误和选项.我相信将来会删除这些错误.因此,如果对于API和您使用的选项,会发生一些错误,请修改googleapis的版本,然后重试.通过修改版本解决的情况如下.
  • How do I update my google sheet in v4?
  • Create a gmail filter with Gmail API nodejs, Error: Filter doesn't have any criteria
  • Insufficient Permission when trying to create a folder on Google Drive via API(v3)
  • Youtube Data API V3 - Error fetching video with google.youtube.videos.list()
  • Google drive API - Cannot read property 'OAuth2' of undefined
  • How to run a Google App Script using Google API Service Library (Node.js)

如果这对您的情况没有帮助,对不起.

If this was not useful for your situation, I'm sorry.

此修改如何?在我的环境中,我确认此修改适用于v30.0.0版的googleapis.

How about this modification? In my environment, I confirmed that this modification worked by googleapis of v30.0.0.

drive.files.get({fileId: fileId, alt: 'media'})
.on('end', () => {
    console.log('Done');
})
.on('error', err => {
    console.log('Error', err);
})
.pipe(dest);

收件人:

drive.files.get({fileId: fileId, alt: 'media'}, {responseType: 'stream'},
    function(err, res){
        res.data
        .on('end', () => {
            console.log('Done');
        })
        .on('error', err => {
            console.log('Error', err);
        })
        .pipe(dest);
    }
);

这篇关于Node.js Google云端硬盘api下载文件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆