-
Notifications
You must be signed in to change notification settings - Fork 0
/
classroom.html
96 lines (87 loc) · 3.9 KB
/
classroom.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Google Classroom Posts</title>
<script src="https://apis.google.com/js/api.js"></script>
</head>
<body>
<h1>Google Classroomの投稿を取得</h1>
<button id="authorize_button">ログインして取得</button>
<button id="signout_button" style="display:none;">ログアウト</button>
<div id="content"></div>
<script>
const CLIENT_ID = '358501638228-fitpf06l9vdecp6bc5ffutb3qh3o14i7.apps.googleusercontent.com';
const API_KEY = 'AIzaSyAVbjBwhBx9khnJdQEmHTyVb8mDi1E7PK8';
const DISCOVERY_DOCS = ["https://classroom.googleapis.com/$discovery/rest?version=v1"];
const SCOPES = "https://www.googleapis.com/auth/classroom.coursework.me.readonly";
let authorizeButton = document.getElementById('authorize_button');
let signoutButton = document.getElementById('signout_button');
let content = document.getElementById('content');
function handleClientLoad() {
gapi.load('client:auth2', initClient);
}
function initClient() {
gapi.client.init({
apiKey: API_KEY,
clientId: CLIENT_ID,
discoveryDocs: DISCOVERY_DOCS,
scope: SCOPES
}).then(() => {
const authInstance = gapi.auth2.getAuthInstance();
authInstance.isSignedIn.listen(updateSigninStatus);
updateSigninStatus(authInstance.isSignedIn.get());
authorizeButton.onclick = handleAuthClick;
signoutButton.onclick = handleSignoutClick;
}).catch(error => console.error("Error initializing Google API client:", error));
}
function updateSigninStatus(isSignedIn) {
if (isSignedIn) {
authorizeButton.style.display = 'none';
signoutButton.style.display = 'block';
listCoursework();
} else {
authorizeButton.style.display = 'block';
signoutButton.style.display = 'none';
content.innerHTML = '';
}
}
function handleAuthClick() {
gapi.auth2.getAuthInstance().signIn();
}
function handleSignoutClick() {
gapi.auth2.getAuthInstance().signOut();
}
function listCoursework() {
gapi.client.classroom.courses.list().then(response => {
const courses = response.result.courses;
if (courses && courses.length > 0) {
courses.forEach(course => {
gapi.client.classroom.courses.courseWork.list({
courseId: course.id
}).then(courseworkResponse => {
const coursework = courseworkResponse.result.courseWork;
if (coursework && coursework.length > 0) {
coursework.forEach(item => {
const div = document.createElement('div');
div.innerHTML = `
<h2>${item.title}</h2>
<p>${item.description || "No description"}</p>
`;
content.appendChild(div);
});
} else {
console.log(`No coursework found for course: ${course.name}`);
}
});
});
} else {
content.innerHTML = '<p>No courses found.</p>';
}
}).catch(error => console.error("Error fetching courses:", error));
}
handleClientLoad();
</script>
</body>
</html>