0

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;
}
jchleb
  • 27
  • 6
  • 3
    Although I'm not sure whether this is the direct solution of your issue, how about confirming whether Google Classroom API was enabled at Advanced Google services, again? [Ref](https://developers.google.com/apps-script/guides/services/advanced) – Tanaike Mar 31 '20 at 01:33
  • Thanks, Tanaike. How do I confirm? I’ve always run my script for the first time, and there’s been an automatic prompt for permissions on my account. I can see that there is no scope for Classroom in the case of the second script project, but I don’t know how to get that. In Google docs, I haven’t found examples for doing this with Gscript, only Python examples. I’m hoping to call this script from a Gsheet, as I’ve done before, but getting the Classroom info from that starting point has me stumped. – jchleb Mar 31 '20 at 11:31
  • 1
    Thank you for replying. You can confirm whether the API is enabled by 2 patterns. 1. You can do it at "Resources > Advanced Google services" on the script editor. [Ref](https://developers.google.com/apps-script/guides/services/advanced#enabling_advanced_services) 2. You can do it at the manifest file (`appsscript.json`). [Ref](https://developers.google.com/apps-script/manifest) – Tanaike Mar 31 '20 at 12:01

1 Answers1

0

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

jchleb
  • 27
  • 6