我正在尝试使用Google Drive API(v3)获取Google Drive中文件的直接下载链接,但是我也试图在不公开共享文件的情况下执行此操作。这是我尝试过的:
https://www.googleapis.com/drive/v3/files/**FILE_ID**?alt=media&supportsAllDrives=True&includeItemsFromAllDrives=True&key=**API_KEY**
现在,如果文件是公共共享的,则可以使用。但是,当文件未公开共享时,您会收到以下消息:
{'error': {'errors': [{'domain': 'global', 'reason': 'notFound', 'message': 'File not found: 10k0Qogwcz7k0u86m7W2HK-LO7bk8xAF8.', 'locationType': 'parameter', 'location': 'fileId'}], 'code': 404, 'message': 'File not found: 10kfdsfjDHJ38-UHJ34D82.'}}
经过一番谷歌搜索后,我发现堆栈上有一个帖子,说我需要在访问令牌中添加一个请求标头,但这不起作用,应用程序只是挂起
这是完整的代码:
### SETTING UP GOOGLE API
scopes = 'https://www.googleapis.com/auth/drive'
store = file.Storage('storage.json')
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', scopes)
credentials = tools.run_flow(flow, store)
accessToken = credentials.access_token
refreshToken = credentials.refresh_token
drive = build('drive', 'v3', credentials=credentials)
### SENDING REQUEST
req_url = "https://www.googleapis.com/drive/v3/files/"+file_id+"?alt=media&supportsAllDrives=True&includeItemsFromAllDrives=True&key="+GOOGLE_API_KEY
headers={'Authorization': 'Bearer %s' % accessToken}
request_content = json.loads((requests.get(req_url)).content)
print(request_content)
------------------编辑: ------------------
我已经很接近答案了,但是我似乎无法弄清楚为什么这行不通。
因此,我之前已经弄清楚了会alt=media
为该文件生成下载链接,但是当该文件为私有文件时,它将无法正常工作。
我刚刚发现您可以添加&access_token=....
访问私有文件,因此我想到了以下API调用:
https://www.googleapis.com/drive/v3/files/**FILE_ID**?supportsAllDrives=true&alt=media&access_token=**ACCESS_TOKEN**&key=**API_KEY**
当我在浏览器上转到该URL时,收到以下消息:
We're sorry...
... but your computer or network may be sending automated queries. To protect our users, we can't process your request right now.
我感到困惑,因为如果删除alt=media
,我可以调用该请求,并且获得有关该文件的一些元数据。
我相信您的目标如下。
I'm trying to get the direct download link for a file in Google Drive using the Google Drive API (v3),
,我知道您要检索webContentLink
。webContentLink
的文件是Google Docs文件以外的文件。https://www.googleapis.com/drive/v3/files/**FILE_ID**?alt=media&supportsAllDrives=True&includeItemsFromAllDrives=True&key=**API_KEY**
返回File not found
。我认为此问题的原因是由于此。headers
未使用。因此,在这种情况下,不使用访问令牌。includeItemsFromAllDrives
。credentials.access_token
。这个怎么样?如果我的理解正确,请尝试将修改为accessToken = credentials.token
。webContentLink
。因此,在这种情况下,必须将字段值设置为fields=webContentLink
。修改脚本后,它如下所示。
file_id = '###' # Please set the file ID.
req_url = "https://www.googleapis.com/drive/v3/files/" + file_id + "?supportsAllDrives=true&fields=webContentLink"
headers = {'Authorization': 'Bearer %s' % accessToken}
res = requests.get(req_url, headers=headers)
obj = res.json()
print(obj.get('webContentLink'))
或者,您可以drive = build('drive', 'v3', credentials=credentials)
在脚本中使用,也可以使用以下脚本。
file_id = '###' # Please set the file ID.
drive = build('drive', 'v3', credentials=credentials)
request = drive.files().get(fileId=file_id, supportsAllDrives=True, fields='webContentLink').execute()
print(request.get('webContentLink'))
因此,请注意以上几点。
*
用于时fields
,可以检索所有文件元数据。不幸的是,在这种情况下,webContentLink
不能使用。因为在这种大文件的情况下,将webContentLink
被重定向。因此,我认为公开共享文件并使用API密钥的方法适合实现您的目标。但是,您不能公开共享文件。
在这种情况下,作为一种解决方法,我建议使用此方法。此方法是“一次性下载Google云端硬盘”。在Google云端硬盘中,下载了公共共享文件后,即使在下载过程中删除了文件许可权,也可以运行下载。此方法使用此方法。
在此示例脚本中,使用了API密钥。
在这种解决方法中,Google Apps脚本用作服务器端。请创建一个独立的脚本。如果您想直接创建它,请访问https://script.new/。在这种情况下,如果您尚未登录Google,则会打开“登录”屏幕。因此,请登录到Google。这样,将打开Google Apps脚本的脚本编辑器。
请复制以下脚本并将其粘贴到脚本编辑器中。那时,请将API密钥设置key
为function中的变量doGet(e)
。
在这里,请在中设置API密钥doGet(e)
。在此Web Apps中,当输入的API密钥相同时,将运行脚本。
function deletePermission() {
const forTrigger = "deletePermission";
const id = CacheService.getScriptCache().get("id");
const triggers = ScriptApp.getProjectTriggers();
triggers.forEach(function(e) {
if (e.getHandlerFunction() == forTrigger) ScriptApp.deleteTrigger(e);
});
const file = DriveApp.getFileById(id);
file.setSharing(DriveApp.Access.PRIVATE, DriveApp.Permission.NONE);
}
function checkTrigger(forTrigger) {
const triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++) {
if (triggers[i].getHandlerFunction() == forTrigger) {
return false;
}
}
return true;
}
function doGet(e) {
const key = "###"; // <--- API key. This is also used for checking the user.
const forTrigger = "deletePermission";
var res = "";
if (checkTrigger(forTrigger)) {
if ("id" in e.parameter && e.parameter.key == key) {
const id = e.parameter.id;
CacheService.getScriptCache().put("id", id, 180);
const file = DriveApp.getFileById(id);
file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
var d = new Date();
d.setMinutes(d.getMinutes() + 1);
ScriptApp.newTrigger(forTrigger).timeBased().at(d).create();
res = "https://www.googleapis.com/drive/v3/files/" + id + "?alt=media&key=" + e.parameter.key;
} else {
res = "unavailable";
}
} else {
res = "unavailable";
}
return ContentService.createTextOutput(res);
}
这是python的示例脚本。在测试之前,请确认以上脚本已部署为Web Apps。并请设置Web Apps的URL,文件ID和您的API密钥。
import requests
url1 = "https://script.google.com/macros/s/###/exec"
url1 += "?id=###fileId###&key=###your API key###"
res1 = requests.get(url1)
url2 = res1.text
res2 = requests.get(url2)
with open("###sampleFilename###", "wb") as f:
f.write(res2.content)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句