简体   繁体   English

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

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

I'm new to Google APIs and a noob at node.JS.我是 Google API 的新手,也是 node.JS 的新手。 I can't figure out creating a course doesn't work.我无法弄清楚创建课程不起作用。

The script for creating a course is a modified version of the apps script example available on the Google Developer website.创建课程的脚本是 Google Developer 网站上提供的应用程序脚本示例的修改版本。

Help is highly appriciated as I am a young student trying to make my own e-learning platform based off Google Classroom and other aleardy made solutions.非常感谢帮助,因为我是一名年轻的学生,正试图基于谷歌课堂和其他已有的解决方案制作我自己的电子学习平台。

Am I missing something?我错过了什么吗?

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

Can you try separating the call for listCourses and createCourse.您可以尝试将 listCourse 和 createCourse 的调用分开吗?

authorize() accepts 2 arguments: credentials and callback. 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);
});

I tried to create a course using your request body and it was successful.我尝试使用您的请求正文创建一门课程,并且成功。 courses.create 课程.创建

You might also want to combine your listCourses() and createCourse() into a single function so that you don't need to get authentication token for each request.您可能还希望将listCourses()createCourse()组合成一个 function,这样您就不需要为每个请求获取身份验证令牌。

(UPDATE): (更新):

Can you try this:你可以试试这个:

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

Due to lack of node.js examples in the Classroom API, I tried to look for other Google API which only sends a request body.由于教室 API 中缺少 node.js 示例,我试图寻找其他仅发送请求正文的 Google API。

I found this Calendar API Freebusy.query , and based on this sample node.js code , It was called like this:我找到了这个日历 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) });

request body was set as a resource parameter请求正文被设置为资源参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM