2017-08-28 214 views
0

我是Google Classroom API的新成員,我試圖使用JavaScript在本地計算機上獲取課程詳細信息。Google課堂API使用JavaScript獲取課程詳細信息

在我的代碼中,我使用的功能是getCourse(course_id),這是我單擊課程列表中的一個按鈕時的訪問。有消息時發生

但是當我嘗試運行此則有錯誤:

需要的路徑參數ID丟失。

<!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> 

請幫助解決這個問題。

+0

我已經在功能getCourse(改爲GET方法的參數)等爲{ 'ID':字符串(courceId)}或{ 'ID':字符串(courceId),「OWNERID ':'me'}但是在兩種情況下,狀態碼=> 400和「狀態」=>「INVALID_ARGUMENT」都有錯誤。現在該怎麼辦? –

回答

0

這裏有一個片段我做的,你可以參考:

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

     }); 
     } 

這裏,getResult變量保存在 Collection: courses file resource中發現的性質。 這裏是我的示例HTML輸出:

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

感謝它的工作 –