Google Classroom API:无法接收有关课程作业和学生提交的推送通知(COURSE_WORK_CHANGES)

问题描述 投票:-1回答:1

我们受到严重封锁。我们遵循了以下文档(除其他文档外)来设置发布/订阅管道,创建服务帐户,分配权限并使用正确的范围和供稿类型进行注册。

https://developers.google.com/classroom/guides/push-notifications

因此,通过编程,我们可以使用API​​在.net中执行以下操作:

  1. 我们可以创建课程

  2. 我们可以为给定的课程编号创建注册

  3. 我们为已创建注册的课程创建/更新课程。

到目前为止一切顺利,

但是,我们不会收到有关创建/更新课程工作的通知。

一些代码以提高清晰度:

        ServiceAccountCredential credential = new ServiceAccountCredential(
        new ServiceAccountCredential.Initializer("[email protected]")
       {
          User = "impersonated user",
          Scopes = new string[] { "https://www.googleapis.com/auth/classroom.coursework.students" ,
                                  "https://www.googleapis.com/auth/classroom.courses",
                                  "https://www.googleapis.com/auth/classroom.push-notifications" }}
       .FromPrivateKey("My private key"));

        //Authorize request
        var result = credential.RequestAccessTokenAsync(CancellationToken.None).Result;

        var service = new ClassroomService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
        });

        // We get courses
        var courses = service.Courses.List().Execute();

        // We get one course for registration
        var course = courses.Courses.First();

        // We create registration
        var registration = service.Registrations.Create(new Google.Apis.Classroom.v1.Data.Registration()
        {
            Feed = new Google.Apis.Classroom.v1.Data.Feed()
            {
                FeedType = "COURSE_WORK_CHANGES",
                CourseWorkChangesInfo = new Google.Apis.Classroom.v1.Data.CourseWorkChangesInfo()
                {
                    CourseId = course.Id
                },
            },
            CloudPubsubTopic = new Google.Apis.Classroom.v1.Data.CloudPubsubTopic()
            {
                TopicName = "projects/precise-asset-259113/topics/test"
            },

        });

        //Successful response - We get a registrationID
        var response = registration.Execute();

        var courseWork = new CourseWork()
        {
                 CourseId = course.Id,
                 Title = "Ver Test",
                 Description = "Calculus",
                 WorkType = "ASSIGNMENT",
                 MaxPoints = 20.0,
                 State = "PUBLISHED",
                 AlternateLink = "www.uni.com",
                 CreatorUserId = course.OwnerId,
                 CreationTime = DateTime.UtcNow,
                 DueTime = new TimeOfDay() { Hours = 5, Minutes = 10, Nanos = 10, Seconds = 10 },
                 DueDate = new Date() { Day = 3, Month = 12, Year = 2019 },
                 Assignment = new Assignment() { StudentWorkFolder = new DriveFolder() { AlternateLink = "Somewhere", Title = "My Calculus" } }
        };

        //Create course work for the course that we registered
        var courseWorkResponse = service.Courses.CourseWork.Create(courseWork, course.Id).Execute();

       SubscriberServiceApiClient subscriber = SubscriberServiceApiClient.Create();
       SubscriptionName subscriptionName = new SubscriptionName("precise-asset-259113", "test");

         PullResponse pullResponse = subscriber.Pull(
            subscriptionName, returnImmediately: true, maxMessages: 20);

       // Check for push notifications BUT ....NADA!!!
        foreach (ReceivedMessage msg in pullResponse.ReceivedMessages)
        {
            string text = Encoding.UTF8.GetString(msg.Message.Data.ToArray());
            Console.WriteLine($"Message {msg.Message.MessageId}: {text}");
        }

您能帮忙吗?

谢谢

google-cloud-platform google-oauth google-cloud-pubsub service-accounts google-classroom
1个回答
0
投票

为了确保您能收到通知,您需要更改几件事:

  1. 您需要先创建订阅,然后才能发送将生成发布/订阅消息的请求。仅保证在订阅成功创建后发布的[消息才被订阅者接收。
  2. returnImmediately设置为true的单个提取请求不太可能返回任何消息,即使已发布消息也是如此。设置此属性后,如果服务器内存中没有立即到达的消息,则返回空响应。您应始终将returnImmediately设置为false。即使有可用的消息,这仍然不能保证消息会在单个请求/响应中返回,但是这样做更有可能。
  3. 理想情况下,您将使用asynchronous client library,它会打开Cloud Pub / Sub服务的流并在消息可用时立即接收消息。如果要直接使用同步Pull方法,则需要同时保持许多同步状态,以确保以最小的延迟传递消息。一旦收到任何未解决的请求的PullResponse,您应该立即打开另一个请求以替换它。异步客户端库的目标是防止必须手动执行所有这些步骤以确保有效交付。
© www.soinside.com 2019 - 2024. All rights reserved.