Here is a short example using CORS.
The ID of the file is stored in variable <theID
> ; this ID is unique and won't change as long as the file is not deleted (a trashed file is not deleted).
- First
gapi.client.request
retrieves the downloadUrl property ; the returned value is a short lived value ;
- Then, the
callback: function
sends an authenticated request in order to retrieve the contents of the file, thanks to its <downloadUrl> ;
- And the winner is… returned via
onreadystatechange = function( theProgressEvent )
.
.
gapi.client.request({
'path': '/drive/v2/files/'+theID,
'method': 'GET',
callback: function ( theResponseJS, theResponseTXT ) {
var myToken = gapi.auth.getToken();
var myXHR = new XMLHttpRequest();
myXHR.open('GET', theResponseJS.downloadUrl, true );
myXHR.setRequestHeader('Authorization', 'Bearer ' + myToken.access_token );
myXHR.onreadystatechange = function( theProgressEvent ) {
if (myXHR.readyState == 4) {
// 1=connection ok, 2=Request received, 3=running, 4=terminated
if ( myXHR.status == 200 ) {
// 200=OK
console.log( myXHR.response );
}
}
}
myXHR.send();
}
});
tested with "Chrome 20.0.1132.57 m" and "Firefox 14.0.1"