首页 > 解决方案 > Google Drive API 无法使用 files.get、request 或 google request 下载文件

问题描述

我使用客户端 js Google Drive API,我似乎无法读取/下载由同一 api 先前创建的文本文件。

我在 files/fileID 和 webContentLink url 上尝试了 files.get、手动请求和 google 请求。

webContentLink 如果我在浏览器中打开它但不是通过请求打开它,即使我在浏览器中获得授权(存在 cookie)并在请求中发送令牌(url 和 header)。

fileId
wcLink = webContentLink
oToken = oauthToken

测试1

谷歌在 webContentLink 上的请求

返回 404 未找到

调用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=特征

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

测试2

使用 .execute 对文件/fileId 的 Google 请求

返回 200 OK,响应未定义且 raw 正文为空,内容长度为 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
})

测试3

drive.files.get 上的 fileId

返回 200 OK,响应结果:false,正文:''

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

测试4

使用 .then 对文件/fileId 的 Google 请求

返回 200 OK,响应结果:false,正文:''

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

测试5

对文件/fileId 的 http 请求

返回 403 禁止,CORS 警告

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()

测试6

webContentLink 上的 http 请求,链接中带有访问令牌

返回 401 Unauthorized, CORS 警告

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()

测试7

webContentLink 上的 http 请求,链接中没有访问令牌,没有 responseType

我不明白这与上面的示例有何不同,在上面的示例中,我在 url 和 header 中都传递 access_token 并获得 401 unathorized 但在这里我只传递了 header 并获得 200 ok ...

返回 200 OK,CORS 警告

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

全部回复:1 2,在网络选项卡中:3

标签: javascriptgoogle-drive-apixmlhttprequestcors

解决方案


似乎我的上传代码无法正常工作并且正在创建一个空文件。

我已经手动向其中添加了内容,现在可以在测试 2(原始)、3(响应)和 4(响应)的主体中正确检索到它


推荐阅读