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 coming 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.
You can send JavaScript typed arrays as binary data as well. This is building a 512-byte array of 8-bit integers and sending it; you can use any binary data you'd like, of course.
Binary is simply a set or a collection of 1 and 0 . Each number in a binary, each 1 and 0 in a set are called a bit. Computer converts the data to this binary format to store and perform operations. For example, the following are five different binaries: 10, 01, 001, 1110, 00101011.
Was 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)
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With