如何使用Google Drive API通过Javascript下载文件 [英] How to use Google Drive API to download files with Javascript
问题描述
我想用JavaScript API从谷歌驱动器下载文件。我设法使用 gapi.client.drive.files
请求验证并加载文件列表。然而,我坚持下载这些文件。
我尝试下载文件:
var request = gapi.client.drive.files.get({
fileId:id,
alt:'media'
});
request.execute(function(resp){
console.log(resp);
});
试图运行以上时出现这些错误:
(403)用户尚未授予应用程序336423653212对文件0B0UFTVo1BFVmeURrWnpDSloxQlE的读取权限。
$ b $(400)错误请求
我认识到,谷歌驱动器文件(谷歌文档,谷歌幻灯片)返回403错误。
我是新手。任何帮助和答案真的很感谢。
更新0
有关处理API错误的文档,这里是解释的一部分对于 400错误
这可能意味着必填字段或参数尚未如果提供的话,所提供的
值是无效的,或者提供的字段的组合是
无效。
这是因为我的参数对象中有 alt:'media'
>。
我尝试过 gapi.client.drive.files.export
,但它也不起作用,它返回(403)权限不足
,但我的Google云端硬盘帐户拥有这些文件的编辑权限。这是我的代码:
var request = gapi.client.drive.files.get({
fileId:element .id,
});
request.then(function(resp){
console.log(resp.result);
type = resp.result.mimeType;
id = resp.result.id;
var request = gapi.client.drive.files.export({
fileId:id,
mimeType:type
})
request.execute(function(resp ){
console.log(resp);
});
});
更新1
基于 abielita的回答,我试图做出一个授权的HTTP请求,但它不下载该文件。它实际上在 ______________________________________________________ 我发现我可以使用文件 来自Google云端硬盘类型(Google文档,Google表格, 非Google云端硬盘类型文件将包含 我的代码: 基于此文档,如果您使用 请查看此处执行文件下载的示例与我们的Drive API客户端库 对于错误(403)权限不足,可能这是个问题使用您的访问令牌,而不是您的项目配置。 当您检索访问令牌时没有请求您需要的范围时,将返回权限不足错误。您可以通过将access_token传递给此端点来检查您请求的范围: https: //www.googleapis.com/oauth2/v1/tokeninfo?access_token=ACCESS_TOKEN 查看以下链接: 请记住,您正在上传到服务帐户的Google云端硬盘帐户。如果您希望能够从您自己的Google云端硬盘帐户中看到它,您将不得不插入权限。给你自己的访问 I want to download files from google drive with javascript API. I have managed to authenticate and load list of files using I have these errors when trying to run the above: I recognize that the files which aren't google drive file (google doc, google slide) return the 403 error. Update 0 From Google Drive documentation about Handling API Error, here is part of the explanation for This can mean that a required field or parameter has not been provided, the
value supplied is invalid, or the combination of provided fields is
invalid. This is because I have Update 1 Based on abielita's answer, I have tried to make an authorized HTTP request but it doesn't download the file. It actually returns the file information in ______________________________________________________
I found out that I can actually retrieve URLs of all those files from the folder using files' A file which is from Google Drive type (Google Doc, Google Sheet,
etc...) will have A non Google Drive type file will have My code:
Based from this documentation, if you're using Check here the examples of performing a file download with our Drive API client libraries. For the error (403) Insufficient Permission, maybe this is a problem with your access token, not with your project configuration. The insufficient permissions error is returned when you have not requested the scopes you need when you retrieved your access token. You can check which scopes you have requested by passing your access_token to this endpoint: https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=ACCESS_TOKEN Check these links: Google drive Upload returns - (403) Insufficient Permission Remember you are uploading to the service accounts google drive account. If you want to be able to see it from your own Google drive account you are going to have to do an insert of the permissions. to give yourself access 这篇关于如何使用Google Drive API通过Javascript下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! XMLHttpRequest $ c中返回了
响应
和 responseText
属性中的文件信息
$ p $ function test(){
var accessToken = gapi.auth.getToken()。access_token ;
var xhr = new XMLHttpRequest();
xhr.open(GET,https://www.googleapis.com/drive/v3/files/\"+'1A1RguZpYFLyO9qEs-EnnrpikIpzAbDcZs3Gcsc7Z4nE',true);
xhr.setRequestHeader('Authorization','Bearer'+ accessToken);
xhr.onload = function(){
console.log(xhr);
}
xhr.send('alt = media');
}
<
webViewLink
从文件夹中检索所有这些文件的URL。或 webViewContent
属性。
等)的文件将会具有 webViewLink
属性。 webViewLink
会在Google云端硬盘中打开
文件。
webContentLink
。
webContentLink
将下载该文件。
var request = gapi.client.drive.files.list({
q:''0Bz9_vPIAWUcSWWo0UHptQ005cnM 'in parents,//文件夹ID
'字段':files(id,name,webContentLink,webViewLink)
});
request.execute(function(resp){
console.log(resp);
}
alt = media
,则需要对该文件的 资源URL
并包含查询参数 alt =媒体
。
GET https://www.googleapis .com / drive / v3 / files / 0B9jNhSvVjoIVM3dKcGRKRmVIOVU?alt = media
Authorization:Bearer ya29.AHESVbXTUv5mHMo3RYfmS1YJonjzzdTOFZwvyOAUVhrs
字符串fileId =0BwwA4oUTeiV1UVNwOHItT0xfa2M;
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().get(fileId)
.executeMediaAndDownloadTo(outputStream);
gapi.client.drive.files
request. However, I stuck at downloading those files.
My attempt to download the file:var request = gapi.client.drive.files.get({
fileId:id,
alt:'media'
});
request.execute(function(resp){
console.log(resp);
});
(403) The user has not granted the app 336423653212 read access to the file 0B0UFTVo1BFVmeURrWnpDSloxQlE.
(400) Bad Request
I am new to this. Any help and answer is really appreciated.400 errors
alt:'media'
in my parameter object.
I tried gapi.client.drive.files.export
, but it doesn't work either and it returns (403) Insufficient Permission
although my Google Drive account has the edit permission for those files. Here is my code:var request = gapi.client.drive.files.get({
fileId:element.id,
});
request.then(function(resp){
console.log(resp.result);
type = resp.result.mimeType;
id = resp.result.id;
var request = gapi.client.drive.files.export({
fileId:id,
mimeType:type
})
request.execute(function(resp){
console.log(resp);
});
});
response
and responseText
attribute in the XMLHttpRequest
object. function test() {
var accessToken = gapi.auth.getToken().access_token;
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.googleapis.com/drive/v3/files/"+'1A1RguZpYFLyO9qEs-EnnrpikIpzAbDcZs3Gcsc7Z4nE', true);
xhr.setRequestHeader('Authorization','Bearer '+accessToken);
xhr.onload = function(){
console.log(xhr);
}
xhr.send('alt=media');
}
webViewLink
or webViewContent
attributes.
webViewLink
attribute. A webViewLink
will open
the file in Google Drive.webContentLink
. A
webContentLink
will download the file.var request = gapi.client.drive.files.list({
q:"'0Bz9_vPIAWUcSWWo0UHptQ005cnM' in parents", //folder ID
'fields': "files(id, name, webContentLink, webViewLink)"
});
request.execute(function(resp) {
console.log(resp);
}
alt=media
, you need to make an authorized HTTP GET request to the file's resource URL
and include the query parameter alt=media
.GET https://www.googleapis.com/drive/v3/files/0B9jNhSvVjoIVM3dKcGRKRmVIOVU?alt=media
Authorization: Bearer ya29.AHESVbXTUv5mHMo3RYfmS1YJonjzzdTOFZwvyOAUVhrs
String fileId = "0BwwA4oUTeiV1UVNwOHItT0xfa2M";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().get(fileId)
.executeMediaAndDownloadTo(outputStream);