谷歌云端硬盘 API javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11315962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Drive API javascript
提问by Sandro
I'm trying to use the Google drive to list files.
我正在尝试使用 Google 驱动器来列出文件。
Using the answer in https://stackoverflow.com/a/11280257I found a problem that I can't discover the reason.
使用https://stackoverflow.com/a/11280257 中的答案我发现了一个我无法找到原因的问题。
var clientId = '*********.apps.googleusercontent.com';
var apiKey = '##########';
var scopes = 'https://www.googleapis.com/auth/drive';
function handleClientLoad() {
gapi.client.setApiKey(apiKey);
window.setTimeout(checkAuth,1);
}
function checkAuth() {
gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true},handleAuthResult);
}
function handleAuthResult(authResult) {
var authorizeButton = document.getElementById('authorize-button');
if (authResult && !authResult.error) {
authorizeButton.style.visibility = 'hidden';
makeApiCall();
}
else {
authorizeButton.style.visibility = '';
authorizeButton.onclick = handleAuthClick;
}
}
function handleAuthClick(event) {
gapi.auth.authorize({client_id: clientId, scope: [scopes], immediate: false}, handleAuthResult);
return false;
}
function makeApiCall() {
gapi.client.load('drive', 'v2', makeRequest);
}
function makeRequest()
{
var request = gapi.client.drive.files.list({'maxResults': 5 });
request.execute(function(resp) {
for (i=0; i<resp.items.length; i++) {
var titulo = resp.items[i].title;
var fechaUpd = resp.items[i].modifiedDate;
var userUpd = resp.items[i].lastModifyingUserName;
var userEmbed = resp.items[i].embedLink;
var userAltLink = resp.items[i].alternateLink;
var fileInfo = document.createElement('li');
fileInfo.appendChild(document.createTextNode('TITLE: ' + titulo + ' - LAST MODIF: ' + fechaUpd + ' - BY: ' + userUpd ));
document.getElementById('content').appendChild(fileInfo);
}
});
}
I have this error:
我有这个错误:
Uncaught TypeError: Cannot read property 'files' of undefined
in the line
在行中
var request = gapi.client.drive.files.list({'maxResults': 5 });
回答by Sandro
Using
使用
var request = gapi.client.request({
'path': '/drive/v2/files',
'method': 'GET',
'params': {'maxResults': '1'}
});
instead of
代替
var request = gapi.client.drive.files.list({'maxResults': 5 });
resolved the problem!
解决了问题!
回答by Steve Bazyl
Code looks OK and you're correctly waiting until gapi.client.load completes. Might just be an error with loading the Drive JS files or some other issue (maybe bad JS file cached?). I modified your example a little bit to run on jsfiddle, take a look at http://jsfiddle.net/Rbg44/4/for the full example:
代码看起来没问题,您正确地等待直到 gapi.client.load 完成。可能只是加载 Drive JS 文件时出错或其他一些问题(可能缓存了错误的 JS 文件?)。我稍微修改了您的示例以在 jsfiddle 上运行,请查看http://jsfiddle.net/Rbg44/4/以获取完整示例:
HTML:
HTML:
<button id="authorize-button">Authorize</button>
<div id="content">Files:</div>
JS:
JS:
var CLIENT_ID = '...';
var API_KEY = '...';
var SCOPES = '...';
function handleClientLoad() {
gapi.client.setApiKey(API_KEY);
window.setTimeout(checkAuth,1);
}
function checkAuth() {
var options = {
client_id: CLIENT_ID,
scope: SCOPES,
immediate: true
};
gapi.auth.authorize(options, handleAuthResult);
}
function handleAuthResult(authResult) {
var authorizeButton = document.getElementById('authorize-button');
if (authResult && !authResult.error) {
authorizeButton.style.visibility = 'hidden';
makeApiCall();
} else {
authorizeButton.style.visibility = '';
authorizeButton.onclick = handleAuthClick;
}
}
function handleAuthClick(event) {
var options = {
client_id: CLIENT_ID,
scope: SCOPES,
immediate: false
};
gapi.auth.authorize(options, handleAuthResult);
return false;
}
function makeApiCall() {
gapi.client.load('drive', 'v2', makeRequest);
}
function makeRequest() {
var request = gapi.client.drive.files.list({'maxResults': 5 });
request.execute(function(resp) {
for (i=0; i<resp.items.length; i++) {
var titulo = resp.items[i].title;
var fechaUpd = resp.items[i].modifiedDate;
var userUpd = resp.items[i].lastModifyingUserName;
var userEmbed = resp.items[i].embedLink;
var userAltLink = resp.items[i].alternateLink;
var fileInfo = document.createElement('li');
fileInfo.appendChild(document.createTextNode('TITLE: ' + titulo +
' - LAST MODIF: ' + fechaUpd + ' - BY: ' + userUpd ));
document.getElementById('content').appendChild(fileInfo);
}
});
}
$(document).ready(function() {
$('#authorize-button').on('click', handleAuthClick);
$.getScript('//apis.google.com/js/api.js', function() {
gapi.load('auth:client', handleClientLoad);
});
});
Can you check in your browsers dev tools if there is any sort of issue in the request made when you call gapi.client.load()?
如果在调用 gapi.client.load() 时发出的请求中存在任何类型的问题,您能否检查浏览器开发工具?
回答by traeper
You need to write this :
你需要这样写:
gapi.client.load('drive', 'v2', null);