Search code examples
google-apps-scriptgoogle-sheetsoauthgoogle-classroomscopes

How do I resolve OAuth scopes to GSheets and in call out to a GClassroom function, in order to grab assignment data and post to GSheet?


I don't know JSON, so I'm trying to code this with GScript. I want to combine a call out to this function that gets Classroom info from a working script function that posts array info to a GSheet.

The first time I ran the script below, I triggered the API authentication and got the information I needed, although only in Logger.

var email = "my_email@something.org";

function countWork(email) {
  var courseId = "valid_courseId";  
  var data = ""; // String of resulting information from loop below
  var assignments = Classroom.Courses.CourseWork.list(courseId);
  var length = assignments.courseWork.length;
  // Loop to gather info
    for (j = 0; j < length; j++) {
    var assignment = assignments.courseWork[j];        
    var title = assignment.title;
    var created = assignment.creationTime;
    Logger.log('-->Assignment No. %s -->%s -->(%s)',j+1,title,created);  
    }
    return data;
}

But for some reason, I can't OAuth scopes on this version of the script where I've substituted the array I need for posting to GSheet. I get the error message "Classroom is not defined (line 7...)." What do I need to do so Classroom.Courses.etc will be recognized?

var email = "my_email@something.org";

function extractAssignmentData(email) {
  var courseId = "valid_courseId"; //
  var data = []; // Array of resulting information from loop below
  var assignments = Classroom.Courses.CourseWork.list(courseId); // error: Classroom is not defined (line 7)
  var length = assignments.courseWork.length;

  // Loop to gather data
    for (j = 0; j < length; j++) {
    var assignment = assignments.courseWork[j];  
    // types of information: description, creationTime, updateTime, dueDate, dueTime, workType 

    var title = assignment.title;
    var created = assignment.creationTime;
    var info = [j+1,title,created];
    data.push(info);   
    }
    return data;
}

Solution

  • Thanks so much, Tanaike, for your helpful responses!

    Based on your suggestions, I was able to find this post, which explicitly described how to consult the manifest file, and how to incorporate scopes into the appsscript.json.

    I'm still not sure why the first version of the script triggered the scope

    "https://www.googleapis.com/auth/classroom.coursework.students"

    while the second instead added this one:

    "https://www.googleapis.com/auth/classroom.coursework.me.readonly"

    But, since I now know how to add what I need and can access the Classroom info I need, it's a mute point. Thanks, again! (I'm not sure how to mark your comment as the answer to my question -- you should get the points!)