2

现在,我的谷歌课堂 API 可以正常工作,以获取我拥有的每门课程的名称和部分。我想在每门课程的列表中列出作业。我怎样才能做到这一点?

我目前有:

// Your Client ID can be retrieved from your project in the Google
  // Developer Console, https://console.developers.google.com
  var CLIENT_ID = '<CLIENTID>';

  var SCOPES = ["https://www.googleapis.com/auth/classroom.courses.readonly"];

  /**
   * Check if current user has authorized this application.
   */
  function checkAuth() {
    gapi.auth.authorize(
      {
        'client_id': CLIENT_ID,
        'scope': SCOPES.join(' '),
        'immediate': true
      }, handleAuthResult);
  }

  /**
   * Handle response from authorization server.
   *
   * @param {Object} authResult Authorization result.
   */
  function handleAuthResult(authResult) {
    var authorizeDiv = document.getElementById('authorize-div');
    if (authResult && !authResult.error) {
      // Hide auth UI, then load client library.
      authorizeDiv.style.display = 'none';
      loadClassroomApi();
    } else {
      // Show auth UI, allowing the user to initiate authorization by
      // clicking authorize button.
      authorizeDiv.style.display = 'inline';
    }
  }

  /**
   * Initiate auth flow in response to user clicking authorize button.
   *
   * @param {Event} event Button click event.
   */
  function handleAuthClick(event) {
    gapi.auth.authorize(
      {client_id: CLIENT_ID, scope: SCOPES, immediate: false},
      handleAuthResult);
    return false;
  }

  /**
   * Load Classroom API client library.
   */
  function loadClassroomApi() {
    gapi.client.load('classroom', 'v1', listCourses);
  }

  /**
   * Print the names of the first 10 courses the user has access to. If
   * no courses are found an appropriate message is printed.
   */
  function listCourses() {
    var request = gapi.client.classroom.courses.list({
      pageSize: 10
    });

    request.execute(function(resp) {
      var courses = resp.courses;

      if (courses.length > 0) {
        for (i = 0; i < courses.length; i++) {
          var course = courses[i];
          var div = document.createElement('div');
          div.className = 'class="col-md-4"';
          div.innerHTML = '<div class="col-md-4"> \
          <div class="jumbotron">  \
          <h2>' + course.name + '</h2> \
          <p>' + course.section + '</p> \
          </div> \
          </div> \
          </div>';
          document.getElementById('output').appendChild(div);
        }
      } else {
        appendPre('No courses found.');
      }

    });
  }

  /**
   * Append a pre element to the body containing the given message
   * as its text node.
   *
   * @param {string} message Text to be placed in pre element.
   */
  function appendPre(message) {
    var pre = document.getElementById('output');
    var textContent = document.createTextNode(message + '\n');
    pre.appendChild(textContent);
  }
4

1 回答 1

0

我无法确认 Google API 的 Javascript 客户端是否已经具有 Classroom API 功能(因为它仍处于测试阶段),但您可以尝试调用

gapi.client.[classroom/classroom.courses].courseWork.list({..parameters..},function(){...callback...});

或者,直接在 Javascript 中调用REST API,而不使用 JS 客户端。

于 2016-09-27T09:39:20.450 回答