[英]Google classroom API - creating a course with Active status and using service account errors out
[英]Why does creating a course on the Google Classroom API doesn't work?
我是 Google API 的新手,也是 node.JS 的新手。 我无法弄清楚创建课程不起作用。
创建课程的脚本是 Google Developer 网站上提供的应用程序脚本示例的修改版本。
非常感谢帮助,因为我是一名年轻的学生,正试图基于谷歌课堂和其他已有的解决方案制作我自己的电子学习平台。
我错过了什么吗?
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);
});
}```
您可以尝试将 listCourse 和 createCourse 的调用分开吗?
authorize()
接受 2 arguments:凭证和回调。
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,这样您就不需要为每个请求获取身份验证令牌。
你可以试试这个:
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);
});
}
由于教室 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) });
请求正文被设置为资源参数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.