无法使用API​​ - node.js从Google云端硬盘下载文件

Har*_*ngh 6 javascript node.js google-drive-api

我正在尝试使用node.js 使用Google SDK API从Google驱动器下载文件.

但我无法在服务器端写入/保存文件 - node.js

码:-

var GoogleTokenProvider = require("refresh-token").GoogleTokenProvider,
    async = require('async'),
    fs = require("fs"),
    request = require('request'),
    _accessToken;

var _XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var https = require('https');

const CLIENT_ID = "";
const CLIENT_SECRET = "";
const REFRESH_TOKEN = '';
const ENDPOINT_OF_GDRIVE = 'https://www.googleapis.com/drive/v2';

async.waterfall([
        //-----------------------------
        // Obtain a new access token
        //-----------------------------
        function(callback) {
            var tokenProvider = new GoogleTokenProvider({
                'refresh_token': REFRESH_TOKEN,
                'client_id': CLIENT_ID,
                'client_secret': CLIENT_SECRET
            });
            tokenProvider.getToken(callback);
        },

        //--------------------------------------------
        // Retrieve the children in a specified folder
        // 
        // ref: https://developers.google.com/drive/v2/reference/files/children/list
        //-------------------------------------------
        function(accessToken, callback) {
            _accessToken = accessToken;
            request.get({
                'url': ENDPOINT_OF_GDRIVE + '/files?' + "q='root' in parents  and (mimeType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')",
                'qs': {
                    'access_token': accessToken
                }
            }, callback);
        },

        //----------------------------
        // Parse the response
        //----------------------------
        function(response, body, callback) {
            var list = JSON.parse(body);
            if (list.error) {
                return callback(list.error);
            }
            callback(null, list.items[0]);
        },

        //-------------------------------------------
        // Get the file information of the children.
        //
        // ref: https://developers.google.com/drive/v2/reference/files/get
        //-------------------------------------------
        function(children, callback) {

            var xhr = new _XMLHttpRequest();
            xhr.open('GET', children.downloadUrl);
            xhr.setRequestHeader('Authorization', 'Bearer ' + _accessToken);
            xhr.onload = function() {
                console.log("xhr.responseText", xhr.responseText)
                fs.writeFile("download.docx", xhr.responseText)
                callback(xhr.responseText);
            };
            xhr.onerror = function() {
                callback(null);
            };
            xhr.send();
        }
    ],
    function(err, results) {
        if (!err) {
            console.log(results);
        }
    });
Run Code Online (Sandbox Code Playgroud)

我在控制台中得到这个: - xhr.responseText的内容是这样的

???h???E6M??~??3?3??9?? ? ????/2?:?????4??]??I?R????
$SB6Q???c???H?=;+
????q?3Td???@!T??hEl_?|?I????h(?^:??[h?D???f????*??????M?
?1??"N???o?]?7U$??A6??????W??k`?f????K?Z?^??0{<Z???]F?????

                             ???J?A?????a?}7?
"???H?w"??????w????? ?P?^????O????;?<??aY??`G?kxm??PY?[??g
G?ino?/<???<?1???A$>"f3??\??T???I S???????W?????Y
Run Code Online (Sandbox Code Playgroud)

请帮助我知道我从Drive Api获取的数据格式是什么,并以哪种格式编写,以便我得到一个完整的.docx文件

编辑

如果它可以帮助我下载文件(.docx),我可以使用除xmlRequest之外的任何方法.

Rob*_*ann 10

看来,node-XMLHttpRequest不支持二进制下载 - 请参阅此问题.您所看到的是文件的二进制内容转换为String,在JavaScript中,它是二进制数据的不可逆和破坏性过程(这意味着您无法将字符串转换回缓冲区并获取与原始内容相同的数据).

使用请求,您可以这样下载二进制文件:

var request = require('request')
  , fs = require('fs')

request.get(
  { url: 'your-file-url'
  , encoding: null    // Force Request to return the data as Buffer
  , headers:
    { Authorization: 'Bearer ' + accessTokenHere
    }
  }
, function done (err, res) {
    // If all is well, the file will be at res.body (buffer)
    fs.writeFile('./myfile.docx', res.body, function (err) {
      // Handle err somehow
      // Do other work necessary to finish the request
    })
  }
)
Run Code Online (Sandbox Code Playgroud)

注意:这会将整个文件缓冲到内存中,然后才能保存到磁盘.对于小文件,这很好,但对于较大的文件,您可能希望将其实现为流式下载.这个SO问题已经回答了,我建议你看看.

有关如何授权请求的详细信息,请参阅Google Developers文档.