Google Classroom 外掛程式現已全面開放開發人員使用!詳情請參閱
外掛程式說明文件。
Classroom API 的推播通知
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您可以使用 Registrations
集合中的方法,在 Classroom 中的資料發生變更時接收通知。
本文將提供概念總覽,並簡要說明如何開始接收推播通知。
Classroom 推播通知總覽
Classroom API 推播通知功能可讓使用 Classroom API 的應用程式在 Classroom 中資料異動時訂閱通知。通知會傳送至 Cloud Pub/Sub 主題,通常會在變更後的幾分鐘內傳送。
如要接收推播通知,您必須設定 Cloud Pub/Sub 主題,並在為通知的適當動態饋給建立註冊時提供該主題名稱。
以下是本文件中所用重要概念的定義:
- 目的地是指通知傳送的位置。
- 動態饋給是第三方應用程式可訂閱的通知類型。例如:「課程 1234 的學生名單變更」。
- 註冊是 Classroom API 的操作說明,可將特定動態饋給中的通知傳送至目的地。
建立動態饋給註冊後,該註冊的 Cloud Pub/Sub 主題會收到該動態饋給的通知,直到註冊到期為止。註冊有效期限為一週,但您可以在到期前隨時向 registrations.create()
提出相同要求,延長註冊期限。
Cloud Pub/Sub 主題只會接收關於資源的通知,這些資源必須是您在建立註冊時提供的憑證可查看的資源。舉例來說,如果使用者撤銷應用程式的權限,或是遭到移除老師身分,系統就不會再傳送通知。
動態饋給類型
Classroom API 目前提供三種類型的動態饋給:
- 每個網域都有一個學生名單變更動態消息,當學生和老師加入或離開該網域的課程時,系統就會顯示通知。
- 每門課程都有一個課程學生名單變更動態消息,當學生和老師加入或離開該課程時,系統就會顯示通知。
- 每門課程都有一個「課程作業變更」動態消息,當課程作業或學生提交物件在該課程中建立或修改時,系統就會顯示通知。
設定 Cloud Pub/Sub 主題
通知會傳送至 Cloud Pub/Sub 主題。您可以透過 Cloud Pub/Sub 接收 Webhook 通知,也可以輪詢訂閱端點。
如要設定 Cloud Pub/Sub 主題,您必須執行下列操作:
- 請務必符合 Cloud Pub/Sub 先決條件。
- 設定 Cloud Pub/Sub 用戶端。
- 查看 Cloud Pub/Sub 價格,並為您的雲端控制台專案啟用帳單功能。
您可以在「開發人員控制台」中建立 Cloud Pub/Sub 主題 (最簡單的方法),也可以透過指令列工具 (用於簡單的程式設計用途) 或使用 Cloud Pub/Sub API 建立。請注意,Cloud Pub/Sub 只允許有限數量的主題,因此如果應用程式變得熱門,使用一個主題接收所有通知,就能確保不會發生縮放問題。
在 Cloud Pub/Sub 中建立訂閱項目,告訴 Cloud Pub/Sub 如何傳送通知。
最後,在註冊推播通知前,您需要授予推播通知服務帳戶 (classroom-notifications@system.gserviceaccount.com
) 權限,以便發布至主題。
注意:如果您授予推播通知服務帳戶發布至 Cloud Pub/Sub 主題的權限,則可透過開發人員控制台專案提出要求的使用者就能判斷該帳戶是否存在,並註冊接收通知。許多應用程式會在用戶端端儲存 OAuth 用戶端 ID,因此使用者可能會透過 Developer Console 專案提出要求。如果您符合上述情況,且擔心使用者會傳送不必要的通知至您的 Cloud Pub/Sub 主題,或是知道您用於推播通知的 Cloud Pub/Sub 主題名稱,建議您考慮從其他開發人員控制台專案註冊推播通知。
註冊應用程式以接收通知
有了 Classroom API 推播通知服務帳戶可發布的主題後,您就可以使用 registrations.create()
方法註冊通知。registrations.create()
方法會驗證推播通知服務帳戶是否可以存取提供的 Cloud Pub/Sub 主題。如果推播通知服務帳戶無法存取主題 (例如主題不存在,或您未授予該帳戶該主題的發布權限),這個方法就會失敗。
授權
如同所有 Classroom API 呼叫,對 registrations.create()
的呼叫必須經過授權,才能使用授權權杖。這個驗證權杖必須包含推播通知權限範圍 (https://www.googleapis.com/auth/classroom.push-notifications
),以及查看哪些通知傳送資料所需的任何權限範圍。
- 對於名單變更動態饋給,這表示名單範圍或 (理想情況下) 其唯讀變化版本 (
https://www.googleapis.com/auth/classroom.rosters.readonly
或 https://www.googleapis.com/auth/classroom.rosters
)。
- 對於課程作業變更動態消息,這表示課程作業範圍的「學生」版本,或 (理想情況下) 其唯讀變化版本 (
https://www.googleapis.com/auth/classroom.coursework.students.readonly
或 https://www.googleapis.com/auth/classroom.coursework.students
)。
如要傳送通知,應用程式必須保留經授權使用者授予的 OAuth 授權,且該授權必須具備必要的範圍。如果使用者中斷應用程式連線,通知就會停止。請注意,目前系統不支援全網域委派權限的功能。如果您嘗試僅使用全網域委派權限註冊通知,系統會傳回 @MissingGrant
錯誤。
接收通知
通知會以 JSON 編碼,並包含:
以下程式碼片段示範通知範例:
{
"collection": "courses.students",
"eventType": "CREATED",
"resourceId": {
"courseId": "12345",
"userId": "45678"
}
}
通知也具有 registrationId
訊息屬性,其中包含導致通知的註冊 ID,可與 registrations.delete()
搭配使用,從通知中取消註冊。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-05-26 (世界標準時間)。
[null,null,["上次更新時間:2025-05-26 (世界標準時間)。"],[],[],null,["# Push notifications in the Classroom API\n\nYou can use the methods on the\n[`Registrations`](/workspace/classroom/reference/rest/v1/registrations)\ncollection to receive notifications when data changes in Classroom.\n\nThis article provides a conceptual overview along with simple instructions on\nhow to start receiving push notifications.\n\nOverview of Classroom push notifications\n----------------------------------------\n\nThe Classroom API push notifications feature allows applications using the\nClassroom API to subscribe for notifications when data changes in Classroom.\nNotifications are delivered to a [Cloud\nPub/Sub](https://cloud.google.com/pubsub) topic, usually within a few minutes of\nthe change.\n\nTo receive push notifications, you need to [set up a Cloud Pub/Sub\ntopic](#topic-setup) and provide that topic's name when you create a\n**registration** for the appropriate **feed** of notifications.\n\nBelow are definitions of key concepts used in this documentation:\n\n- A **destination** is a place where notifications are sent.\n- A **feed** is a type of notifications that a third party application can subscribe to. For example, \"roster changes for course 1234\".\n- A **registration** is an instruction to the Classroom API to deliver notifications from a particular *feed* to a *destination*.\n\nOnce you create a registration for a feed, that registration's Cloud Pub/Sub\ntopic receives notifications from that feed until it expires. Your registration\nlasts a week, but you can extend it at any point before it expires by making an\nidentical request to `registrations.create()`.\n\nYour Cloud Pub/Sub topic only receives notifications about resources that you\ncan view with the credentials you supply when creating a registration. For\nexample, if the user revokes permission from your application or is removed as a\nteacher, notifications are no longer delivered.\n\n### Types of feeds\n\nThe Classroom API currently offers three types of feed:\n\n- Each domain has a **roster changes for domain** feed, which exposes notifications when students and teachers join and leave courses in that domain.\n- Each course has a **roster changes for course** feed, which exposes notifications when students and teachers join and leave courses in that course.\n- Each course has a **course work changes for course** feed, which exposes notifications when any course work or student submission objects are created or modified in that course.\n\nSetting up a Cloud Pub/Sub topic\n--------------------------------\n\nNotifications are delivered to Cloud Pub/Sub topics. From Cloud Pub/Sub, you can\nreceive notifications on a webhook, or by polling a subscription endpoint.\n\nTo set up a Cloud Pub/Sub topic, you need to do the following:\n\n1. Make sure you fulfill the [Cloud Pub/Sub\n Prerequisites](https://cloud.google.com/pubsub/prereqs).\n2. [Set up a Cloud Pub/Sub client](https://cloud.google.com/pubsub/configure).\n3. Review the [Cloud Pub/Sub pricing](https://cloud.google.com/pubsub/pricing), and enable billing for your Developer Console project.\n4. Create a Cloud Pub/Sub topic [in the Developer\n Console](https://cloud.google.com/pubsub/docs/quickstart-console#create_a_topic)\n (easiest), [via the command line\n tool](https://cloud.google.com/pubsub/docs/quickstart-cli#use_the_gcloud_command-line_tool)\n (for simple programmatic use) or using the [Cloud Pub/Sub\n API](https://cloud.google.com/pubsub/docs/quickstart-client-libraries#create_a_topic_and_a_subscription).\n Note that Cloud Pub/Sub [only allows a limited number of\n topics](https://cloud.google.com/pubsub/quotas), so using one topic to\n receive all your notifications ensures you do not hit scaling issues if your\n application becomes popular.\n\n5. [Create a Subscription in Cloud\n Pub/Sub](https://cloud.google.com/pubsub/docs/subscriber), to tell Cloud\n Pub/Sub how to deliver your notifications.\n\n6. Finally, before registering for Push Notifications, you need to [grant the\n Push Notifications service account\n (`classroom-notifications@system.gserviceaccount.com`)\n permission](https://cloud.google.com/pubsub/docs/access_control#console) to\n publish to your topic.\n\nNOTE: If you grant the Push Notifications service account permission to publish\nto your Cloud Pub/Sub topic, users who can make requests from your Developer\nConsole project will be able to determine that it exists, and register for\nnotifications to it. Many applications store OAuth Client IDs on the client\nside, so end-users might be able to make requests from your Developer Console\nproject. If this applies to you, and you are concerned about end-users sending\nunwanted notifications to your Cloud Pub/Sub topic or knowing the names of Cloud\nPub/Sub topics that you use for push notifications, you should consider\nregistering for push notifications from a different Developer Console project.\n\nRegister your application for notifications\n-------------------------------------------\n\nOnce you have a topic that the Classroom API push notifications service account\ncan publish to, you can register for notifications, using the\n[`registrations.create()`](/workspace/classroom/reference/rest/v1/registrations/create)\nmethod. The `registrations.create()` method validates that the provided Cloud\nPub/Sub topic can be reached by the push notifications service account. The\nmethod fails if the push notifications service account cannot reach the topic;\nfor example, if the topic does not exist or you have not granted it publish\npermission on that topic.\n\n### Authorization\n\nLike all calls to the Classroom API, calls to `registrations.create()` [must be\nauthorized](/workspace/classroom/guides/auth) with an authorization token. This\nauthentication token must include the Push Notifications scope\n(`https://www.googleapis.com/auth/classroom.push-notifications`) and whatever\nscopes are required to view the data about which notifications are being sent.\n\n- For roster change feeds, this means the Rosters scope or (ideally) its read-only variant (`https://www.googleapis.com/auth/classroom.rosters.readonly` or `https://www.googleapis.com/auth/classroom.rosters`).\n- For course work change feeds, this means the \"students\" versions of the course work scope or (ideally) its read-only variant (`https://www.googleapis.com/auth/classroom.coursework.students.readonly` or `https://www.googleapis.com/auth/classroom.coursework.students`).\n\nFor notifications to be delivered, the application must retain an OAuth grant\nfrom the authorized user with the required scopes. If the user disconnects the\napplication, notifications cease. Note that currently, domain-wide delegation of\nauthority is not supported for this purpose. If you attempt to register for\nnotifications using only domain-wide delegated authority, you will receive a\n`@MissingGrant` error.\n\nReceiving notifications\n-----------------------\n\nNotifications are encoded with JSON, and contain:\n\n- The name of the collection containing the resource that changed. For notifications about roster changes, this is either `courses.students` or `courses.teachers`. For course work changes, this is either `courses.courseWork` or `courses.courseWork.studentSubmissions`.\n- Identifiers for the resource that changed, in a map. This map is designed to match the arguments to the `get` method of the appropriate resource. For notifications about roster changes, the `courseId` and `userId` fields will be populated, and can be sent unmodified to [courses.students.get()](/workspace/classroom/reference/rest/v1/courses.students/get) or [courses.teachers.get()](/workspace/classroom/reference/rest/v1/courses.teachers/get). Similarly, changes to the courses.courseWork collection will have `courseId` and `id` fields that can be sent unmodified to [courses.courseWork.get()](/workspace/classroom/reference/rest/v1/courses.courseWork/get) and changes to the courses.courseWork.studentSubmissions collection will have `courseId`, `courseWorkId`, and `id` fields that can be sent unmodified to [courses.courseWork.studentSubmissions.get()](/workspace/classroom/reference/rest/v1/courses.courseWork.studentSubmissions/get).\n\nThe following code snippet demonstrates a sample notification: \n\n {\n \"collection\": \"courses.students\",\n \"eventType\": \"CREATED\",\n \"resourceId\": {\n \"courseId\": \"12345\",\n \"userId\": \"45678\"\n }\n }\n\nNotifications also have a `registrationId` message attribute, containing the\nidentifier for the registration that caused the notification, which can be used\nwith\n[`registrations.delete()`](/workspace/classroom/reference/rest/v1/registrations/delete)\nto unregister from notifications."]]