2016-04-24 45 views
5

Mi sto imbattendo in un problema durante il recupero di un file binario da Drive utilizzando l'API, continuo ad andare in circolo.Lettura di un file binario da Google Drive tramite node.js

Ecco i bit di codice rilevante:

// 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) 
    }); 
} 

Ora, response sembra essere a tornare come una stringa. Quando provo a convertire in esadecimale, diventa matto. C'è un modo per prendere response e portarlo in un Buffer? O è corrotto il sec ho capito da service.files.get?

da noci, voglio dire che

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 - 1.011.010-1111111111111101 - 0-11 (sto usando binario per cercare di vedere ciò che è rotto)

la correttezza esadecimale sarebbe 4D 5A 90 00 03

Edit: per coloro che sono confusi, come se fossi, come 90 diventato fffd è il Unicode replacement character che viene visualizzato quando il valore non mappa a un carattere ASCII.

risposta

3

Era in grado di risolvere questo, finalmente. Le API di Google utilizzano il modulo di richiesta e puoi apply any options che accetta. Per riferimento, è necessario impostare [encoding: null]2, in quanto qualsiasi altra opzione passerà la risposta anche a toString, quindi rovinandola se si sta lavorando con dati binari.

codice di lavoro si trova sotto:

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) 
    }); 
} 
0

Questa risposta si basa su un articolo 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 
    } 
    } 
} 

Se non ottenere un array di byte vostra dovrà convertire la stringa in un ByteArray con il codice qui sotto.

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 
} 
+0

Questo esempio sembra funzionare solo quando si imposta 'responseType = "ArrayBuffer"', qualcosa che non sembrano essere in grado di fare con API Drive. Il tuo esempio di codice restituisce un oggetto generico di lunghezza zero. – Drazisil

Problemi correlati