【发布时间】:2017-11-08 19:49:10
【问题描述】:
我正在尝试使用 Google 的 Picker API 获取文件的直接下载 URL,以便我可以选择一个文件并将此 URL 传递给服务器端代码,以下载该项目的副本并将其存储在服务器上。
我能够通过选择器 API 进行授权,并获取所选文件的信息,包括文件名和预览 URL(在 JSON 响应文档中被混淆地称为“此项目的 URL”:@987654321 @)
我注意到这里有一篇关于使用 Drive API 获取直接下载 URL 的帖子:Get google drive file download URL
但是,当我在我的选择器回调函数中执行此操作时(基于此处的文档:https://developers.google.com/picker/docs/)
我收到以下错误:
"Project [number here] is not found and cannot be used for API calls. If it is recently created, enable Drive API by visiting https://console.developers.google.com/apis/api/drive.googleapis.com/overview?project=[project number here] then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry."
我在我的开发者控制台中启用了 API,并且添加到 JS 的 URL 允许来源。
文档非常混乱,似乎有 3 个版本的 REST API 可用于 Drive,它基于 gapi.auth2 对象,而picker api 使用 gapi.auth 对象。
我不确定在执行 GET 请求之前是否需要再次使用 Google Drive API 进行身份验证。这一切看起来都很混乱,我相信对于一个简单的请求必须有一种更简单的方法!
我的选择器回调函数:
pickerCallback: function(data) {
if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
var doc = data[google.picker.Response.DOCUMENTS][0];
var fileName = doc[google.picker.Document.NAME];
var url = doc[google.picker.Document.URL];
var docId = doc[google.picker.Document.ID];
var request = null;
gapi.client.load('drive', 'v2', function() {
request = gapi.client.drive.files.get({
'fileId': docId
});
request.execute(function(resp){
console.log(resp);
});
});
//Write upload details to page
//Populate hidden field
}
【问题讨论】:
标签: javascript oauth-2.0 google-drive-api