I'm running into a problem getting a binary from Drive using the API, I keep going in circles.
Here are the relevant code bits:
// Load client secrets from a local file.
fs.readFile('client_secret.json', function processClientSecrets(err, content) {
if (err) {
console.log('Error loading client secret file: ' + err);
return;
}
// Authorize a client with the loaded credentials, then call the
// Drive API.
oauth.authorize(JSON.parse(content), dasm.init, driveapi.getFile)
});
driveapi.getFile:
function getFile(auth, cb) {
var service = google.drive('v3');
service.files.get({
auth: auth,
pageSize: 20,
fileId: "0B2h-dgPh8_5CZE9WZVM4a3BxV00",
alt: 'media'
}, function(err, response) {
if (err) {
console.log('The API returned an error: ' + err);
return;
}
cb(response)
});
}
Now, response
appears to be ing back as a string. When I try to convert to hex it goes nuts. Is there any way to to take response
and get it into a Buffer
? Or is it corrupted the sec I get it from service.files.get
?
By nuts, I mean that
console.log(
arrData[0].charCodeAt(0).toString(2),
'-',
arrData[1].charCodeAt(0).toString(2),
'-',
arrData[2].charCodeAt(0).toString(2),
'-',
arrData[3].charCodeAt(0).toString(2),
'-',
arrData[4].charCodeAt(0).toString(2)
)
= 1001101 - 1011010 - 1111111111111101 - 0 - 11 (I'm using binary to try to see what is broken)
The correct hex would be 4D 5A 90 00 03
Edit: For those who are confused, like I was, how 90
became fffd
it's the Unicode replacement character that gets displayed when the value doesn't map to an ASCII char.
I'm running into a problem getting a binary from Drive using the API, I keep going in circles.
Here are the relevant code bits:
// Load client secrets from a local file.
fs.readFile('client_secret.json', function processClientSecrets(err, content) {
if (err) {
console.log('Error loading client secret file: ' + err);
return;
}
// Authorize a client with the loaded credentials, then call the
// Drive API.
oauth.authorize(JSON.parse(content), dasm.init, driveapi.getFile)
});
driveapi.getFile:
function getFile(auth, cb) {
var service = google.drive('v3');
service.files.get({
auth: auth,
pageSize: 20,
fileId: "0B2h-dgPh8_5CZE9WZVM4a3BxV00",
alt: 'media'
}, function(err, response) {
if (err) {
console.log('The API returned an error: ' + err);
return;
}
cb(response)
});
}
Now, response
appears to be ing back as a string. When I try to convert to hex it goes nuts. Is there any way to to take response
and get it into a Buffer
? Or is it corrupted the sec I get it from service.files.get
?
By nuts, I mean that
console.log(
arrData[0].charCodeAt(0).toString(2),
'-',
arrData[1].charCodeAt(0).toString(2),
'-',
arrData[2].charCodeAt(0).toString(2),
'-',
arrData[3].charCodeAt(0).toString(2),
'-',
arrData[4].charCodeAt(0).toString(2)
)
= 1001101 - 1011010 - 1111111111111101 - 0 - 11 (I'm using binary to try to see what is broken)
The correct hex would be 4D 5A 90 00 03
Edit: For those who are confused, like I was, how 90
became fffd
it's the Unicode replacement character that gets displayed when the value doesn't map to an ASCII char.
2 Answers
Reset to default 5Was able to solve this, finally. Google APIs use the request module, and you can apply any options that it accepts. For reference, you will need to set [encoding: null]
2, as any other option will pass the response though toString, thus ruining it if you are working with binary data.
Working code is located below:
function getFile(auth, cb) {
var service = google.drive({
version: 'v3',
encoding: null
});
service.files.get({
auth: auth,
fileId: "0B2h-dgPh8_5CZE9WZVM4a3BxV00",
alt: 'media'
}, function(err, response) {
if (err) {
console.log('The API returned an error: ' + err);
return;
}
cb(response)
});
}
This answer is based on an article in MDN about sending and receiving binary data
function getFile(auth, cb) {
var service = google.drive('v3');
service.files.get({
auth: auth,
pageSize: 20,
fileId: "0B2h-dgPh8_5CZE9WZVM4a3BxV00",
alt: 'media'
}, function(err, response) {
if (err) {
console.log('The API returned an error: ' + err);
return;
}
var arrayBuffer = response;
if (arrayBuffer) {
var byteArray = new Uint8Array(arrayBuffer);
for (var i = 0; i < byteArray.byteLength; i++) {
// do something with each byte in the array
}
}
}
If you do not get a byte array your will have to convert the string to a bytearray with the code below.
var bytes = [];
for (var i = 0, len = response.length; i < len; ++i) {
bytes.push(str.charCodeAt(i));
}
var byteArray = new Uint8Array(bytes);
for (var i = 0; i < byteArray.byteLength; i++) {
// do something with each byte in the array
}