Classroom API のすべての呼び出しと同様に、registrations.create() の呼び出しは承認トークンで承認される必要があります。この認証トークンには、プッシュ通知スコープ(https://www.googleapis.com/auth/classroom.push-notifications)と、送信される通知に関するデータを表示するために必要なスコープを含める必要があります。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-05-26 UTC。"],[],[],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."]]