javascript - 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 未找到
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()
解决方案
似乎我的上传代码无法正常工作并且正在创建一个空文件。
我已经手动向其中添加了内容,现在可以在测试 2(原始)、3(响应)和 4(响应)的主体中正确检索到它
推荐阅读
- spring - 使用 Spring Boot 反应和 rSocket REQUEST_CHANNEL 错误
- android - 压缩图片时出现OutOfMemoryError
- git - 如何将以前的 git 提交拆分为单独的提交?
- flutter - 如何在 Flutter 的 GridView 中设置自定义 UI?
- flutter - 从上传的文件中获取 DownloadURL
- amazon-web-services - NodeJS 或 Nginx 上发生 Access-Control-Allow-Origin cors 错误
- java - 如何在没有整个项目构建(Gradle)的情况下在 IntelliJ 中运行特定的 java 文件?
- r - R中的重新编码系统缺失?
- php - 使用适用于 QuickBooks 的 Consolibyte PHP SDK,查询的正确语法是什么?
- python - heapq.heappush() 是否在未指定的情况下比较 int AND 字符串?