Search code examples
javascriptgoogle-drive-apixmlhttprequestcors

Google Drive API cant download file with files.get, request or google request


Im using the client-side js Google Drive API and i cant seem to read/download a text file created earlier by the same api.

I tried files.get, manual requests and google request, on both the files/fileID and webContentLink urls.

webContentLink works if i open it in browser but not thru request, even tho I'm authorized in browser (cookie present) and sending the token in request (both url and header).

fileId
wcLink = webContentLink
oToken = oauthToken

Test1

Google request on webContentLink

returns 404 not found

calls https://drive.google.com/static/proxy.html?usegapi=1&jsh=m;//scs/apps-static//js/k=oz.gapi.en.jw7XZHvcak8.O/am=wQE/d=1/ct=zgms/rs=AGLTcCOXtLG11kt9d673FzpjO_GiLUGIQA/m=features

let r = gapi.client.request({
    'path': wcLink,
    'method': 'GET',
    'params': {'fileId': fileId, 'alt': 'media'},
    'headers': {'Authorization': 'Bearer ' + oToken }
})
r.execute((response,rawData)=>{ //callback not called at all
    console.log('resp',response)
    console.log('raw',rawData)
})

Test2

Google request on files/fileId, using .execute

returns 200 OK, response undefined and raw has empty body, content-length 0

let r = gapi.client.request({
    'path': 'https://www.googleapis.com/drive/v3/files/'+fileId,
    'method': 'GET',
    'params': {'fileId': fileId, 'alt': 'media'},
    'headers': {'Authorization': 'Bearer ' + oToken }
})
r.execute((response,rawData)=>{
    console.log('resp',response) //undefined
    console.log('raw',rawData) //body:'', content-length:0
})

Test3

drive.files.get on fileId

returns 200 OK, response result: false, body: ''

gapi.client.drive.files.get({
    'fileId': fileId,
    'alt': 'media'
})
.then((response,rawData)=>{
    console.log('resp',response) //result: false, body: ''
    console.log('raw',rawData) //undefined
})

Test4

Google request on files/fileId, using .then

returns 200 OK, response result: false, body: ''

gapi.client.request({
    'path': 'https://www.googleapis.com/drive/v3/files/'+fileId,
    'method': 'GET',
    'params': {'fileId': fileId, 'alt': 'media'},
    'headers': {'Authorization': 'Bearer ' + oToken }
})
.then((response,rawData)=>{
    console.log('resp',response) //result: false, body: ''
    console.log('raw',rawData) //undefined
})

Test5

http request on files/fileId

returns 403 forbidden, CORS warning

var xhr = new XMLHttpRequest();
xhr.open('GET',
'https://www.googleapis.com/drive/v3/files/' + fileId +
'?alt=media&access_token=' + encodeURIComponent(oToken), true)
xhr.responseType = "blob"
xhr.onreadystatechange = ()=>{
    console.log('readyStateChange',xhr)
}
xhr.setRequestHeader('Authorization', 'Bearer ' + oToken)
xhr.send()

Test6

http request on webContentLink with access token in link

returns 401 Unauthorized, CORS warning

let reqUrl = wcLink + '&access_token=' + encodeURIComponent(oToken)
var xhr = new XMLHttpRequest()
xhr.open("GET", reqUrl, true)
xhr.responseType = "blob"
xhr.onreadystatechange = ()=>{
    console.log('readyStateChange',xhr)
}
xhr.setRequestHeader('Authorization', 'Bearer ' + oToken)
xhr.send()

Test7

http request on webContentLink without access token in link, no responseType

I dont understand how this differs from the above example where i pass access_token in both url and header and get 401 unathorized yet here i pass i only header and get 200 ok...

returns 200 OK, CORS warning

let xhr = new XMLHttpRequest()
xhr.onreadystatechange = ()=>{
  console.log('response',xhr)
}
xhr.open('GET', wcLink, true)
xhr.setRequestHeader('Authorization', 'Bearer ' + oToken)
xhr.send()

Whole responses: 1 2, in networking tab: 3


Solution

  • Seems my upload code wasn't working properly and was creating an empty file.

    Ive manually added content to it and its now correctly being retrieved in bodies of tests 2 (in raw), 3 (in response) and 4 (in response)