Google Classroom API to get a course details using JavaScript

风流意气都作罢 提交于 2019-12-11 15:43:50

问题


I am new with Google Classroom API and I am trying to get course details on local machine using JavaScript.

In my code, I am using function getCourse(course_id) and this is access when I click a button from list of courses.

But when I try to run this then there is error occurred with message :

Required path parameter id is missing.

<!DOCTYPE html>
<html>
  <body>

    <button id="authorize-button" style="display: none;">Authorize</button>
    <button id="signout-button" style="display: none;">Sign Out</button>

    <div id="courses-list"><ul></ul></div>

    <script type="text/javascript">

      var CLIENT_ID = '782126680600-9kkg23inbnn9sv8ficcvjci2rgrnd648.apps.googleusercontent.com';


      var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/classroom/v1/rest"];


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

      var authorizeButton = document.getElementById('authorize-button');
      var signoutButton = document.getElementById('signout-button');


      function handleClientLoad() {
        gapi.load('client:auth2', initClient);
      }

      function initClient() {
        gapi.client.init({
          discoveryDocs: DISCOVERY_DOCS,
          clientId: CLIENT_ID,
          scope: SCOPES
        }).then(function () {
          // Listen for sign-in state changes.
          gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);

          // Handle the initial sign-in state.
          updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
          authorizeButton.onclick = handleAuthClick;
          signoutButton.onclick = handleSignoutClick;
        });
      }


      function updateSigninStatus(isSignedIn) {
        if (isSignedIn) {
          authorizeButton.style.display = 'none';
          signoutButton.style.display = 'block';
          createCourse();
          listCourses();
        } else {
          authorizeButton.style.display = 'block';
          signoutButton.style.display = 'none';
        }
      }


      function handleAuthClick(event) {
        gapi.auth2.getAuthInstance().signIn();
      }


      function handleSignoutClick(event) {
        gapi.auth2.getAuthInstance().signOut();
      }


      function appendPre(message) {
        var pre = document.getElementById('content');
        var textContent = document.createTextNode(message + '\n');
        pre.appendChild(textContent);
      }


      function listCourses() {
        gapi.client.classroom.courses.list({
          pageSize: 10
        }).then(function(response) {
            var listHtml= ''
            console.info(response.result);
            var courses = response.result.courses;
            if (courses.length > 0) {
              for (i = 0; i < courses.length; i++) {
                var course = courses[i];
                listHtml += '<li><button class="btn btn-link btn-sm" onclick="getCourse('+course.id+')">'+course.name+'</button></li>';
              }
              $('#courses-list ul').html(listHtml);
          } else {
              $('#courses-list').html('<h1>No courses found.</h1>');
          }
        });
      }
      function getCourse(courseId){
         gapi.client.classroom.courses.get(courseId).then(function(resp){
            console.log(resp);
         });
    }

    </script>

    <script async defer src="https://apis.google.com/js/api.js"
      onload="this.onload=function(){};handleClientLoad()"
      onreadystatechange="if (this.readyState === 'complete') this.onload()">
    </script>
  </body>
</html>

Please help to solve this.


回答1:


Here's a snippet I made that you can refer to:

 function getCourse() {
        gapi.client.classroom.courses.get({
         id: "6981234509"
          pageSize: 10
        }).then(function(response) {

          var getResult = response.result;
          console.log(getResult);
          appendPre("course ID: " + getResult.id);
          appendPre("course name: " + getResult.name);
          appendPre("ownerId: " + getResult.ownerId);
          appendPre("section: " + getResult.section);

        });
      }

Here, getResult variable holds the properties found in Collection: courses file resource. Here's my sample HTML output:

course ID: 6981234509
course name: Test Course
ownerId: 118657867855594024398
section: 9


来源:https://stackoverflow.com/questions/45914125/google-classroom-api-to-get-a-course-details-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!