我是Google Classroom API的新手,我正在尝试使用JavaScript获取本地计算机的课程详情。
在我的代码中,我正在使用函数getCourse(course_id)
,当我从课程列表中单击按钮时,这是访问。
但是当我尝试运行它时,消息出现错误:
缺少必需的路径参数ID。
<!DOCTYPE html>
<html>
<body>
<button id="authorize-button" style="display: none;">Authorize</button>
<button id="signout-button" style="display: none;">Sign Out</button>
<div id="courses-list"><ul></ul></div>
<script type="text/javascript">
var CLIENT_ID = '782126680600-9kkg23inbnn9sv8ficcvjci2rgrnd648.apps.googleusercontent.com';
var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/classroom/v1/rest"];
var SCOPES = "https://www.googleapis.com/auth/classroom.courses";
var authorizeButton = document.getElementById('authorize-button');
var signoutButton = document.getElementById('signout-button');
function handleClientLoad() {
gapi.load('client:auth2', initClient);
}
function initClient() {
gapi.client.init({
discoveryDocs: DISCOVERY_DOCS,
clientId: CLIENT_ID,
scope: SCOPES
}).then(function () {
// Listen for sign-in state changes.
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
// Handle the initial sign-in state.
updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
authorizeButton.onclick = handleAuthClick;
signoutButton.onclick = handleSignoutClick;
});
}
function updateSigninStatus(isSignedIn) {
if (isSignedIn) {
authorizeButton.style.display = 'none';
signoutButton.style.display = 'block';
createCourse();
listCourses();
} else {
authorizeButton.style.display = 'block';
signoutButton.style.display = 'none';
}
}
function handleAuthClick(event) {
gapi.auth2.getAuthInstance().signIn();
}
function handleSignoutClick(event) {
gapi.auth2.getAuthInstance().signOut();
}
function appendPre(message) {
var pre = document.getElementById('content');
var textContent = document.createTextNode(message + '\n');
pre.appendChild(textContent);
}
function listCourses() {
gapi.client.classroom.courses.list({
pageSize: 10
}).then(function(response) {
var listHtml= ''
console.info(response.result);
var courses = response.result.courses;
if (courses.length > 0) {
for (i = 0; i < courses.length; i++) {
var course = courses[i];
listHtml += '<li><button class="btn btn-link btn-sm" onclick="getCourse('+course.id+')">'+course.name+'</button></li>';
}
$('#courses-list ul').html(listHtml);
} else {
$('#courses-list').html('<h1>No courses found.</h1>');
}
});
}
function getCourse(courseId){
gapi.client.classroom.courses.get(courseId).then(function(resp){
console.log(resp);
});
}
</script>
<script async defer src="https://apis.google.com/js/api.js"
onload="this.onload=function(){};handleClientLoad()"
onreadystatechange="if (this.readyState === 'complete') this.onload()">
</script>
</body>
</html>
请帮助解决这个问题。
答案 0 :(得分:0)
这是我制作的一个片段,您可以参考:
function getCourse() {
gapi.client.classroom.courses.get({
id: "6981234509"
pageSize: 10
}).then(function(response) {
var getResult = response.result;
console.log(getResult);
appendPre("course ID: " + getResult.id);
appendPre("course name: " + getResult.name);
appendPre("ownerId: " + getResult.ownerId);
appendPre("section: " + getResult.section);
});
}
此处,getResult
变量包含找到的属性
Collection: courses file resource。
这是我的示例HTML输出:
course ID: 6981234509
course name: Test Course
ownerId: 118657867855594024398
section: 9