为什么在 Google 课堂 API 上创建课程不起作用?



我是Google API的新手,也是节点上的一个noob。JS。我不知道创建一个课程是行不通的。

创建课程的脚本是谷歌开发者网站上可用的应用程序脚本示例的修改版本。

作为一名年轻的学生,我正试图在谷歌课堂和其他手工制作的解决方案的基础上创建自己的电子学习平台,因此我们高度重视帮助。

我是不是错过了什么?

const readline = require('readline');
const {google} = require('googleapis');
const chalk = require('chalk');
const SCOPES = ['https://www.googleapis.com/auth/classroom.courses', 
const TOKEN_PATH = 'token.json';
fs.readFile('credentials.json', (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
// Authorize a client with credentials, then call the Google Classroom API.
authorize(JSON.parse(content), listCourses, createCourse);
});
function authorize(credentials, callback) {
const {client_secret, client_id, redirect_uris} = credentials.installed;
const oAuth2Client = new google.auth.OAuth2(
client_id, client_secret, redirect_uris[0]);
fs.readFile(TOKEN_PATH, (err, token) => {
if (err) return getNewToken(oAuth2Client, callback);
oAuth2Client.setCredentials(JSON.parse(token));
callback(oAuth2Client);
});
}
function getNewToken(oAuth2Client, callback) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
console.log('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter the code from that page here: ', (code) => {
rl.close();
oAuth2Client.getToken(code, (err, token) => {
if (err) return console.error('Error retrieving access token', err);
oAuth2Client.setCredentials(token);
// Store the token to disk for later program executions
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
if (err) return console.error(err);
console.log('Token stored to', TOKEN_PATH);
});
callback(oAuth2Client);
});
});
}
function listCourses(auth) {
const classroom = google.classroom({version: 'v1', auth});
classroom.courses.list({
pageSize: 1234,
}, (err, res) => {
if (err) return console.error(chalk.red('[ERROR] ') + err);
const courses = res.data.courses;
if (courses && courses.length) {
console.log('Courses:');
courses.forEach((course) => {
console.log(`${course.name} (${course.id})`);
});
} else {
console.log('No courses found.');
}
});
}
function createCourse(auth) {
const classroom = google.classroom({version: 'v1', auth});
classroom.courses.create({
name: 'somethin!',
section: 'Period 2',
descriptionHeading: 'somethin',
description: "somethin",
room: '301',
ownerId: 'me',
courseState: 'PROVISIONED',
}, (err, res) => {
if (err) return console.error(chalk.red('[ERROR] ') + err);
});
}```

您可以尝试将listCourses和createCourse的调用分开吗。

authorize()接受两个参数:凭据和回调。

fs.readFile('credentials.json', (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
// Authorize a client with credentials, then call the Google Classroom API.
authorize(JSON.parse(content), listCourses);
authorize(JSON.parse(content), createCourse);
});

我试着用你的请求体创建了一个课程,结果很成功。课程创建

您可能还想将listCourses()createCourse()组合成一个函数,这样就不需要为每个请求获取身份验证令牌。

(更新(:

你能试试这个吗:

function createCourse(auth) {
const classroom = google.classroom({version: 'v1', auth});
classroom.courses.create({
resource: {
name: 'somethin!',
section: 'Period 2',
descriptionHeading: 'somethin',
description: "somethin",
room: '301',
ownerId: 'me',
courseState: 'PROVISIONED',
},
}, (err, res) => {
if (err) return console.error(chalk.red('[ERROR] ') + err);
});
}

由于Classroom API中缺少node.js示例,我尝试寻找其他只发送请求主体的Google API。

我找到了这个日历API Freebusy.query,并基于这个示例node.js代码,它被调用如下:

calendar.freebusy.query(
{
resource: {
timeMin: eventStartTime,
timeMax: eventEndTime,
timeZone: 'America/Denver',
items: [{ id: 'primary' }],
},
},
(err, res) => {
// Check for errors in our query and log them if they exist.
if (err) return console.error('Free Busy Query Error: ', err) });

请求主体被设置为资源参数

最新更新