Permission denied using Classroom.Courses.CourseWo

2019-09-15 14:43发布

问题:

I have been able to move some data from google classroom to a spreadsheet using google script. Specifically anything in classroom courses or course work. I get a permission error when I try to access and thing in coursework.

(method listAssigments works fine needs refinement works slow) (method listGrades gives and permission error at var submissions)

I have set up the API as describe in getting started, but have not added any other authorization.

I originally thought it was because I created it in Google classroom, but when I use the site https://developers.google.com/classroom/reference/rest/v1/courses.courseWork.studentSubmissions/list The try this works fine. When I put the request directly to the web I get the error again.

I know it can work several add-ons get in information, but I can not find any example code that is not python. I am trying to make a simple spreadsheet that will sort the grade as I need them for my grade book. Any work around would be fine. Though I would like to be able to push grades back to google as well. Adding participation points is really slow in classroom, I would rather just copy down and change any anomalies or paste in grade for an another spreadsheet.

I will add the code in case it helps, but I believe it must be some sort of additional authentication or permission needed to provide or add.

function listGrades(courseWorkId, course) {
   var courseId = course.id
   var ss = SpreadsheetApp.getActive().getSheetByName(course.name);
   var submissions = Classroom.Courses.CourseWork.StudentSubmissions.list(4140802199, 4801051201);
   var grade = submissions.studentSubmissions[0].assignedGrade
}

function listAssigments(course) {
   var courseId = course.id
   var courseWork = Classroom.Courses.CourseWork.list(courseId).courseWork
   var ss = SpreadsheetApp.getActive().getSheetByName(course.name);

   for(var i = 0; i < courseWork.length; i++)    {
       var tittle = courseWork[i].title
       var points = courseWork[i].creationTime
       var id = courseWork[i].id
       var id = courseWork[i]   
       ss.getRange(2, i+5).setValue(tittle);
       ss.getRange(3, i+5).setValue(points);
       ss.getRange(1, i+5).setValue(id);
  }

回答1:

OK, I think this a bug with Google Apps Script.

Google Apps Script is supposed to determine the correct permissions automatically by scanning your code before it is executed. Fetching a student's course work using Classroom.Courses.CourseWork.list requires the https://www.googleapis.com/auth/classroom.coursework.student‌​s scope.

However, if you add this request to your code and then go to File, Project properties, Scopes, you'll see that only https://www.googleapis.com/auth/classroom.coursework.me is granted.

I was able to workaround the issue by adding a random call to Classroom.Courses.CourseWork.remove(courseId, id) in my code (you can comment it out so that it isn't actually called... Google Apps Script checks commented out code to determine the correct scopes as well). Google Apps Script correctly detected this request for me and granted the correct permission.

I'll report a bug to Google to see if they can reproduce the same issue we are experiencing...



回答2:

I found a workaround from another post. I did not have a high enough score to post there.

function createCoursework (id) {
  Classroom.Courses.CourseWork.create(id,
    { // doesn't work but triggers permissions correctly
  "courseId": id,
  "title": 'foo',
  "description": 'desc',
    });
}

As Tom Hinkle said. It does not work but it does trigger the authorization you need to access the data.