Google 课堂 API 使用 JavaScript 获取课程详情
Google Classroom API to get a course details using JavaScript
我是 Google 课堂 API 的新手,我正在尝试使用 JavaScript.
在本地计算机上获取课程详细信息
在我的代码中,我使用函数 getCourse(course_id)
,当我从课程列表中单击一个按钮时,这是访问。
但是当我尝试 运行 时出现错误消息:
Required path parameter id is missing.
<!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>
请帮忙解决这个问题。
这是我制作的片段,您可以参考:
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
我是 Google 课堂 API 的新手,我正在尝试使用 JavaScript.
在本地计算机上获取课程详细信息在我的代码中,我使用函数 getCourse(course_id)
,当我从课程列表中单击一个按钮时,这是访问。
但是当我尝试 运行 时出现错误消息:
Required path parameter id is missing.
<!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>
请帮忙解决这个问题。
这是我制作的片段,您可以参考:
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