dimanche 29 mars 2015

Unable to download file from Google Drive using API - node.js

I am trying to download a File from google drive using Google SDK API using node.js.


But I am unable to write/save file at server side - node.js


Code:-



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 = 'http://ift.tt/1yBdjZk';

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: http://ift.tt/1HKbrU3
//-------------------------------------------
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: http://ift.tt/Q3fmNw
//-------------------------------------------
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);
}
});


I am getting this in console:- Content of xhr.responseText is something like that



��▬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


Please help me to know what is the format of the data I am getting from Drive Api and write it in which format so that I got a complete .docx file


Edit


I am open to use any method other than xmlRequest if it helps me downloading the file(.docx).


Bounty For:


A complete working example, which should be capable of downloading/saving/ a (.docx) file from Google Drive at Server using node.js API


Aucun commentaire:

Enregistrer un commentaire