无法使用API-node.js从Google Drive下载文件
Unable to download file from Google Drive using API - node.js
我正在尝试使用google SDK API使用node.js.从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);
}
});
我在控制台中得到这个:-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
请帮助我了解我从Drive Api获得的数据的格式,并用哪种格式编写,以便我获得完整的.docx文件
编辑
我愿意使用xmlRequest以外的任何方法,如果它能帮助我下载文件(.docx).
节点XMLHttpRequest似乎不支持二进制下载-请参阅此问题。您看到的是文件的二进制内容被转换为字符串,在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
})
}
)
注意:这将在将整个文件保存到磁盘之前将其缓冲到内存中。对于小文件,这是可以的,但对于较大的文件,您可能需要考虑将其实现为流式下载。这个SO问题已经回答了这个问题,我建议你看看。
关于如何授权您的请求的更多信息可以在谷歌开发者文档中找到。
完整工作示例:从GoogleDrive下载文件-Node.js API
var GoogleTokenProvider = require("refresh-token").GoogleTokenProvider,
async = require('async'),
fs = require("fs"),
request = require('request'),
_accessToken;
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);
},
//-------------------------------------------
// Get the file information of the children.
//
// ref: https://developers.google.com/drive/v2/reference/files/get
//-------------------------------------------
function(children, callback) {
for(var i=0;i<children.length;i++) {
var file = fs.createWriteStream(children[i].title);
// Downnload and write file from google drive
(function(child) {
request.get(
{ url: child.downloadUrl
, encoding: null // Force Request to return the data as Buffer
, headers:
{ Authorization: 'Bearer ' + _accessToken
}
}
, function done (err, res) {
res.pipe(file)
// If all is well, the file will be at res.body (buffer)
fs.writeFile('./' + child.title, res.body, function (err) {
if(!err) {
console.log('done')
} else {
console.log(err)
}
// Handle err somehow
// Do other work necessary to finish the request
})
}
)
})(children[i])
}
}
],
function(err, results) {
if (!err) {
console.log(results);
}
});
我刚刚遇到了这个问题,我已经包含了一个使用Google API Node.js库使其工作的示例:https://gist.github.com/davestevens/6f376f220cc31b4a25cd
相关文章:
- 如何确保通过电子邮件发送的html文档中的Google Drive链接不会被识别为附件
- 无法使用API-node.js从Google Drive下载文件
- Google Drive:通过javascript访问webContentLink
- Google Drive SDK JavaScript:上传ppt(x)并将其转换为演示文稿(Google幻灯片)
- Google Drive SDK示例python DrEdit不工作(身份验证时闪烁,编辑器未显示)
- 如何通过Google Drive Javascript OAuth2 API创建Google Sheets文档
- 使用Google Drive和Angularjs时,不允许使用请求标头字段Accept Encoding
- 使用JavaScript的Google Drive:处理文件内容
- JavaScript中的Google Drive API请求
- 如何使用PHP从我自己的网站上的Google Drive获取文件和文件夹列表
- NodeJS - Downloading from Google Drive
- Google Drive是否不再允许从其他网站引用JavaScript文件
- 如何从Chrome扩展下载公共Google Drive文件
- 使用Google Drive Viewer显示通过Google Drive API创建的文件
- JSON数据来自未发布的Google Drive电子表格
- 从Google Drive获取文件并隐藏到Blob
- 更新存储在Google Drive上的文件的内容
- 在 Javascript 中使用 Google Drive 共享链接
- 如何在Javascript(Google Drive API)中保存HTTP请求中的URI
- 使用 Google Drive API v3 (javascript) 创建文件