简体   繁体   English

Google Drive API下载NODE.JS示例不起作用

[英]Google Drive API download NODE.JS sample doesn't work

Here is my code based on Google: "Here are examples of performing a file download with our Drive API client libraries." 这是我基于Google的代码:“以下是使用我们的Drive API客户端库执行文件下载的示例。” NODE.JS NODE.JS

const fs = require('fs')
const google = require('googleapis')
const googleAuth = require('google-auth-library')

const SCOPES = ['https://www.googleapis.com/auth/drive'] // Full, permissive scope to access all of a user's files, excluding the Application Data folder. Request this scope only when it is strictly necessary.
const TOKEN_DIR = './credentials/'
const TOKEN_PATH = TOKEN_DIR + 'token4joeDlEs6.json'
const CLIENT_SECRET_FILEPATH = 'client_secret.json'

var clientSecretStr = fs.readFileSync(CLIENT_SECRET_FILEPATH).toString('utf-8')
var tokenStr = fs.readFileSync(TOKEN_PATH).toString('utf-8')


var credentials = JSON.parse(clientSecretStr) 

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)

oauth2Client.credentials = JSON.parse(tokenStr)

var drive = google.drive('v3');

// ["Examples"-NODE.JS tab](https://developers.google.com/drive/v3/web/manage-downloads)
var fileId = 'OBFUSCATED' // Safeway (OBFUSCATED)
var dest = fs.createWriteStream('out/safeway.txt')
drive.files.get({
  auth: oauth2Client,
  fileId: fileId,
  //alt: 'media'
})
    .on('end', function () {
      console.log('Done');
    })
    .on('error', function (err) {
      console.log('Error during download', err);
    })
    .pipe(dest);

RUNS WITHOUT ERRORS: 运行无错误:

$ node joeDlSample.js
Done
$

EXPECTED RESULT 预期结果

out/safeway.txt to contain my shopping list. out / safeway.txt包含我的购物清单。 :) :)

ACTUAL RESULT 实际结果

out/safeway.txt contains out / safeway.txt包含

{
  "access_token" : "OBFUSCATED.OBFUSCATED",
  "expires_in" : 3600,
  "token_type" : "Bearer"
}

2nd RUN CLOSER TO EXAMPLE 第二次更接近示例

I also uploaded a .jpg picture, & uncommented the alt: media line in the code 我还上传了.jpg图片,但未注释alt: media代码中的alt: media

EXPECTED RESULT 预期结果

out/elb.jpg to contain elb picture out / elb.jpg包含elb图片

ACTUAL RESULT 实际结果

out/elb.jpg contains out / elb.jpg包含

{
  "access_token" : "OBFUSCATED.OBFUSCATED",
  "expires_in" : 3600,
  "token_type" : "Bearer"
}

I managed to get Downloading Files in Drive API working by combining the Drive NodeJS Quickstart and the Download Files sample for NodeJS. 通过结合使用Drive NodeJS快速入门NodeJS下载文件示例,我设法使Drive API的下载文件正常工作。

From the NodeJS Quickstart there's this line: 在NodeJS快速入门中,有以下代码行:

authorize(JSON.parse(content), listFiles);

I created my own downloadFile function to download files instead of listing files and modified the above mentioned line to 我创建了自己的downloadFile函数来下载文件而不是列出文件,并将上述行修改为

  authorize(JSON.parse(content), downloadFile);

And here's the downloadFile function part from the NodeJS sample: 这是NodeJS示例的downloadFile函数部分:

function downloadFile(auth){
  var service = google.drive('v3');
  var fileId = 'FILE_ID_OF_SHOPPINGLIST_TEXT_FILE';
  var dest = fs.createWriteStream('/usr/local/google/home/username/Downloads/shoppinglist.txt');

  service.files.get({
      auth: auth,
      fileId: fileId,
      alt: 'media'
  })
      .on('end', function () {
        console.log('Done');
      })
      .on('error', function (err) {
        console.log('Error during download', err);
      })
      .pipe(dest);
}

The downloaded file in my Downloads folder: 我的下载文件夹中的下载文件:

在此处输入图片说明

Works for me :) 为我工作:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从谷歌驱动器 api 下载带有 Node.js 的文件 - How to download a file with Node.js from google drive api Node.js Google Drive api 下载文件错误 - Node.js Google Drive api Download file error 前端的谷歌选择器(用户登录 reactJS)和后端的谷歌驱动器下载(node.js),API v3 - Google picker on frontend (user login in reactJS) and google drive download on backend (node.js), API v3 Google Drive API混乱,如何下载文件夹内容? (的node.js) - Confusion on Google Drive API, how can I download contents of folders? (node.js) 无法使用API​​ - node.js从Google云端硬盘下载文件 - Unable to download file from Google Drive using API - node.js Node.js-通过请求从Google云端硬盘下载文件 - Node.js - Download File from Google Drive via request 在 Node.JS 中从谷歌驱动器下载公共图像 - Download public images from google drive in Node.JS 为什么AWS SAM Node.js示例应用程序不能在Arch Linux LTS上运行? - Why doesn't AWS SAM Node.js sample application work on Arch Linux LTS? 使用Google Drive API和Node.js创建Google文档 - Create a Google Document with Google Drive API and Node.js Node.js - 无法从 Google Drive API 访问我的帐户 - Node.js - Can't access my account from the Google Drive API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM