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

Why does creating a course on the Google Classroom API doesn't work?

我是 Google API 的新手,也是 node.JS 的菜鸟。 我不知道创建课程是行不通的。

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

非常感谢您的帮助,因为我是一名年轻的学生,正在尝试基于 Google 课堂和其他可行的解决方案制作自己的电子学习平台。

我是不是漏掉了什么?

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() 接受 2 个参数:凭据和回调。

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);
});

我尝试使用您的请求正文创建课程并且成功了。 courses.create

您可能还想将 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);
  });
}

由于课堂 node.js 中缺少示例 API,我试图寻找其他 Google API 仅发送请求正文的示例。

我找到了这个日历 API Freebusy.query, and based on this sample node.js code,它的名字是这样的:

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) });

request body was set as a resource parameter