Google drive api 此JS代码返回一个错误,用于使用Google Drive SDK API检索文件夹中的子项
以下代码在chrome控制台中返回错误:Google drive api 此JS代码返回一个错误,用于使用Google Drive SDK API检索文件夹中的子项,google-drive-api,Google Drive Api,以下代码在chrome控制台中返回错误: Uncaught TypeError: Cannot read property 'children' of undefined 初始错误出现在最后一个脚本关闭标记的5行中,稍后(当前)出现在gapi.loaded_0的第38行中 组合这两个代码块,并在下面列出文件夹的子项 <html> <head> <meta http-equiv="Content-type" content="text/html;cha
Uncaught TypeError: Cannot read property 'children' of undefined
初始错误出现在最后一个脚本关闭标记的5行中,稍后(当前)出现在gapi.loaded_0的第38行中
组合这两个代码块,并在下面列出文件夹的子项
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<script type="text/javascript">
var CLIENT_ID = 'semi-secret-clientIdString.apps.googleusercontent.com';
var SCOPES = 'https://www.googleapis.com/auth/drive';
var thisFolderId = 'folderIdString';
/**
* Called when the client library is loaded to start the auth flow.
*/
function handleClientLoad() {
window.setTimeout(checkAuth, 1);
}
/**
* Check if the current user has authorized the application.
*/
function checkAuth() {
gapi.auth.authorize(
{'client_id': CLIENT_ID, 'scope': SCOPES, 'immediate': true},
handleAuthResult);
}
/**
* Called when authorization server replies.
*
* @param {Object} authResult Authorization result.
*/
function handleAuthResult(authResult) {
var authButton = document.getElementById('authorizeButton');
var lister = document.getElementById('Lister');
authButton.style.display = 'none';
lister.style.display = 'none';
if (authResult && !authResult.error) {
// Access token has been successfully retrieved, requests can be sent to the API.
lister.style.display = 'block';
lister.onchange = retrieveAllFilesInFolder(thisFolderId);
} else {
// No access token could be retrieved, show the button to start the authorization flow.
authButton.style.display = 'block';
authButton.onclick = function() {
gapi.auth.authorize(
{'client_id': CLIENT_ID, 'scope': SCOPES, 'immediate': false},
handleAuthResult);
};
}
}
///// Start retrieval function
function retrieveAllFilesInFolder(folderId,callback) {
var retrievePageOfChildren = function(request, result) {
request.execute(function(resp) {
result = result.concat(resp.items);
var nextPageToken = resp.nextPageToken;
if (nextPageToken) {
request = gapi.client.drive.children.list({
'folderId' : folderId,
'pageToken': nextPageToken
});
retrievePageOfChildren(request, result);
} else {
callback(result);
}
});
}
/*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<error on the next line>>>>>>>>>>>>>>>>>>>>>*/
var initialRequest = gapi.client.drive.children.list({
'folderId' : folderId
});
retrievePageOfChildren(initialRequest, []);
}
/////
</script>
<script type="text/javascript" src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>
</head>
<body>
<input id="Lister" type="button" value="clickme" style="display: none" />
<input type="button" id="authorizeButton" style="display: none" value="Authorize" />
</body>
</html>
var CLIENT_ID='semi-secret clientIdString.apps.googleusercontent.com';
var作用域https://www.googleapis.com/auth/drive';
var thisFolderId='folderIdString';
/**
*在加载客户端库以启动身份验证流时调用。
*/
函数handleClientLoad(){
setTimeout(checkAuth,1);
}
/**
*检查当前用户是否已授权应用程序。
*/
函数checkAuth(){
gapi.auth.authorize(
{'client\u id':client\u id,'scope':SCOPES,'immediate':true},
手工(结果);
}
/**
*当授权服务器答复时调用。
*
*@param{Object}authResult授权结果。
*/
函数handleAuthResult(authResult){
var authButton=document.getElementById('authorizeButton');
var lister=document.getElementById('lister');
authButton.style.display='none';
lister.style.display='none';
if(authResult&!authResult.error){
//已成功检索访问令牌,可以向API发送请求。
lister.style.display='block';
lister.onchange=retrieveAllFileInfolder(thisFolderId);
}否则{
//无法检索访问令牌,请显示按钮以启动授权流。
authButton.style.display='block';
authButton.onclick=函数(){
gapi.auth.authorize(
{'client\u id':client\u id,'scope':SCOPES,'immediate':false},
手工(结果);
};
}
}
/////启动检索功能
函数retrieveAllFileInfolder(folderId,回调){
var retrievePageOfChildren=函数(请求、结果){
请求执行(功能(resp){
结果=结果浓度(各项目);
var nextPageToken=resp.nextPageToken;
如果(下一步){
请求=gapi.client.drive.children.list({
“folderId”:folderId,
“pageToken”:nextPageToken
});
检索儿童页面(请求、结果);
}否则{
回调(结果);
}
});
}
/*get chilrend of undefined-此错误具体在哪里?脚本关闭标记gapi.client.drive
中的5行是undefined
(又名,此属性不存在),因此您无法从那里访问儿童。这里有几个相关的问题以及其他人提出的相同问题-我怀疑那里的答案会对您有所帮助。谢谢!这很有帮助,尽管现在我遇到了“超出每日配额使用量”,放下控制台。日志(结果)显示连续对象,它们是相同的().你知道这段代码中的原因吗?