Hiện tại, các nhà phát triển đã có thể sử dụng rộng rãi tiện ích bổ sung của Google Lớp học! Vui lòng xem tài liệu về tiện ích bổ sung để biết thêm thông tin.
Hướng dẫn bắt đầu nhanh giải thích cách thiết lập và chạy một ứng dụng gọi một API Google Workspace. Hướng dẫn bắt đầu nhanh này sử dụng phương pháp xác thực đơn giản phù hợp với môi trường kiểm thử. Đối với môi trường phát hành công khai, bạn nên tìm hiểu về xác thực và uỷ quyền trước khi chọn thông tin đăng nhập để truy cập phù hợp với ứng dụng của bạn.
Trong Apps Script, các hướng dẫn nhanh về Google Workspace sử dụng Dịch vụ nâng cao của Google để gọi API Google Workspace và xử lý một số thông tin chi tiết về quy trình xác thực và uỷ quyền.
Mục tiêu
Định cấu hình môi trường.
Tạo và định cấu hình tập lệnh.
Chạy tập lệnh.
Điều kiện tiên quyết
Tài khoản Google for Education có bật Google Lớp học.
/** * Lists 10 course names and ids. */functionlistCourses(){/** here pass pageSize Query parameter as argument to get maximum number of result * @see https://developers.google.com/classroom/reference/rest/v1/courses/list */constoptionalArgs={pageSize:10// Use other parameter here if needed};try{// call courses.list() method to list the courses in classroomconstresponse=Classroom.Courses.list(optionalArgs);constcourses=response.courses;if(!courses||courses.length===0){console.log('No courses found.');return;}// Print the course names and IDs of the coursesfor(constcourseofcourses){console.log('%s (%s)',course.name,course.id);}}catch(err){// TODO (developer)- Handle Courses.list() exception from Classroom API// get errors like PERMISSION_DENIED/INVALID_ARGUMENT/NOT_FOUNDconsole.log('Failed with error %s',err.message);}}
Nhấp vào biểu tượng Lưu .
Nhấp vào Dự án chưa có tên, nhập Bắt đầu nhanh rồi nhấp vào Đổi tên.
Định cấu hình tập lệnh
Bật Google Lớp học API
Mở dự án Apps Script.
Nhấp vào Trình chỉnh sửacode.
Bên cạnh Dịch vụ, hãy nhấp vào biểu tượng Thêm dịch vụ add .
Chọn Google Classroom API rồi nhấp vào Thêm.
Chạy mẫu
Trong trình chỉnh sửa Apps Script, hãy nhấp vào Chạy.
Trong lần đầu tiên chạy mẫu, bạn sẽ được nhắc uỷ quyền truy cập:
Nhấp vào Xem xét quyền.
Chọn một tài khoản.
Nhấp vào Cho phép.
Nhật ký thực thi của tập lệnh sẽ xuất hiện ở cuối cửa sổ.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-28 UTC."],[],[],null,["Create a\n[Google Apps Script](/apps-script/overview)\nthat makes requests to the Google Classroom API.\n\nQuickstarts explain how to set up and run an app that calls a\nGoogle Workspace API. This quickstart uses a\nsimplified authentication approach that is appropriate for a testing\nenvironment. For a production environment, we recommend learning about\n[authentication and authorization](/workspace/guides/auth-overview)\nbefore\n[choosing the access credentials](/workspace/guides/create-credentials#choose_the_access_credential_that_is_right_for_you)\nthat are appropriate for your app.\n\nIn Apps Script, Google Workspace\nquickstarts use\n[Advanced Google services](/apps-script/guides/services/advanced) to call\nGoogle Workspace APIs and handle some details of the authentication\nand authorization flow.\n\nObjectives\n\n- Configure the environment.\n- Create and configure the script.\n- Run the script.\n\nPrerequisites\n\n\u003cbr /\u003e\n\n- A Google for Education account with Google Classroom enabled.\n\n\u003cbr /\u003e\n\n- Access to Google Drive\n\nCreate the script\n\n1. Create a new script by going to [script.google.com/create](https://script.google.com/create).\n2. Replace the contents of the script editor with the following code:\n\n\nclassroom/quickstart/quickstart.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/classroom/quickstart/quickstart.gs) \n\n```javascript\n/**\n * Lists 10 course names and ids.\n */\nfunction listCourses() {\n /** here pass pageSize Query parameter as argument to get maximum number of result\n * @see https://developers.google.com/classroom/reference/rest/v1/courses/list\n */\n const optionalArgs = {\n pageSize: 10\n // Use other parameter here if needed\n };\n try {\n // call courses.list() method to list the courses in classroom\n const response = Classroom.Courses.list(optionalArgs);\n const courses = response.courses;\n if (!courses || courses.length === 0) {\n console.log('No courses found.');\n return;\n }\n // Print the course names and IDs of the courses\n for (const course of courses) {\n console.log('%s (%s)', course.name, course.id);\n }\n } catch (err) {\n // TODO (developer)- Handle Courses.list() exception from Classroom API\n // get errors like PERMISSION_DENIED/INVALID_ARGUMENT/NOT_FOUND\n console.log('Failed with error %s', err.message);\n }\n}\n```\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n3. Click Save .\n4. Click **Untitled project** , type **Quickstart** , and click **Rename**.\n\n\u003cbr /\u003e\n\nConfigure the script\n\nEnable the Google Classroom API\n\nOpen the Apps Script project.\n\n1. Click **Editor** code.\n2. Next to **Services** , click Add a service add .\n3. Select Google Classroom API and click **Add**.\n\nRun the sample\n\nIn the Apps Script editor, click **Run**.\n\nThe first time you run the sample, it prompts you to authorize access:\n\n1. Click **Review permissions**.\n2. Choose an account.\n3. Click **Allow**.\n\nThe script's execution log appears at the bottom of the window. \ndone It worked! **Great!** Check out the further reading section below to learn more.\nwarning There was a problem **Bummer** , [let us know what went wrong](#). Check out our [troubleshooting](#troubleshooting) section below for some common errors and solutions. If you have found a bug in the code, [report the issue on GitHub](https://github.com/googleworkspace/apps-script-samples/issues) or submit a pull request.\n\nNext steps\n\n- [Google Apps Script Advanced Services documentation](/apps-script/guides/services/advanced)\n- [Try the Google Workspace APIs in the APIs explorer](/workspace/explore)\n- [Troubleshoot authentication and authorization issues](/workspace/classroom/troubleshoot-authentication-authorization)\n- [Classroom API reference documentation](/workspace/classroom/reference)"]]