我如何注册Google Classroom API的推送通知?

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

我想创建一个程序,该程序将从有问题的Google课堂接收通知,并对这些数据进行处理。如何注册Google课堂对事件做出反应?我还没有做任何事情,而且我对Google的API一无所知。我该怎么办?

google-cloud-platform push-notification google-oauth google-classroom
1个回答
0
投票

注册用于推送通知的Google Classroom API的过程包括身份验证,授权,以及向Google解析请求以告知您的Classroom发送这些推送通知的请求。

我强烈建议您具有Java编程语言的Google Cloud平台的基础知识,这与我尝试这样做时不一样。相信我...这不好玩。我确实相信您会充分理解这个概念,以便能够将其转换为您选择的语言,这是我在Java中使用IntelliJ IDEA作为我的IDE进行的。

除Google Classroom API之外,Google为其收藏夹提供了另一项服务,称为“发布/订阅”。发布/订阅代表发布者/订阅者。如果您熟悉队列的工作方式,可以将Pub / Sub视为一种完善的队列系统。您有一个发布者,该发布者在“主题”上发布消息,并且该主题的订阅者将从该主题中提取消息,然后选择是否“确认”该消息。确认消息会将其从队列中删除。例如,发布者代码将消息“ Hello,World”发送到主题。该消息将保留在主题中,直到该主题的订阅者选择拉出消息,阅读“ Hello,World”并确认消息为止,因此在拉出消息时不会再次弹出。发布者可以发送任意数量的消息。发布者可以发送10条消息,而订阅者可以选择将它们全部拉出并一次遍历或一次遍历。

这适用于此系统,因为您将使用Google Classroom API的内置功能,该功能允许该API充当“发布者”并向您选择的主题发送更新消息。然后,您将有一个单独的应用程序随时检查更新。为了现在进行简化,您告诉Classroom API“请向该主题发送更新消息。我只希望在教师以任何方式编辑课程作业目录时进行更新”。该请求后将带有Classroom API,并且每当老师进行编辑,发布,删除等操作时,程序就会向您的主题发送消息。

如果您的课堂发布者每天发送5个更新,则您将有5条可拉出的消息发送到您的主题,该主题的任何订阅程序都可以拉出并确认。

如果您不够了解,您认为。请继续之前,请先对Google Cloud Pub / Sub进行一些研究,因为这样做基本上围绕此服务进行。

让我们逐步进行此操作...

  1. 创建一个新项目
  2. 启用教室API和PubSub API
  3. 启用计费
  4. 转到“ IAM和管理员”
  5. 将所有者权限授予“ [email protected]
  6. 使用“用户数据”为Classroom API和“基于UI的平台”设置凭据
  7. 设置同意屏幕。现在就添加一个应用程序名称。
  8. 创建凭据作为“ OAuth客户端ID”
  9. 选择应用程序类型>其他。不要介意客户名称
  10. 下载JSON文件。将其重命名为“ credentials_temp.json”
  11. 创建一个基于Gradle的Java项目(我正在使用IntelliJ)。群组ID:临时工件ID:临时。项目名称:temp
  12. 将其添加到build.gradle中的“依赖项”下
  13. compile 'com.google.api-client:google-api-client:1.23.0'
    compile 'com.google.oauth-client:google-oauth-client-jetty:1.23.0'
    compile 'com.google.apis:google-api-services-classroom:v1-rev135-1.23.0'
    
  1. 将build.gradle中的sourceCompatibility变量设置为11
  2. 导入这些更改(右下角可能会有一个小方框,其中有“导入更改”作为选项)
  3. 将凭证文件放入src / main / resources
  4. 在src / main / java中,创建一个新类。将其命名为“ TempTest.java”
  5. 使用我为您编写的这段代码
  6. import com.google.api.client.auth.oauth2.Credential;
    import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
    import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
    import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
    import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
    import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
    import com.google.api.client.http.javanet.NetHttpTransport;
    import com.google.api.client.json.JsonFactory;
    import com.google.api.client.json.jackson2.JacksonFactory;
    import com.google.api.client.util.store.FileDataStoreFactory;
    import com.google.api.services.classroom.Classroom;
    import com.google.api.services.classroom.model.*;
    
    import java.io.*;
    import java.security.GeneralSecurityException;
    import java.util.ArrayList;
    import java.util.List;
    
    import static com.google.api.services.classroom.ClassroomScopes.all;
    
    public class TempTest {
        private static final String APPLICATION_NAME = "Google Classroom API Java Quickstart";
        private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
        private static final String TOKENS_DIRECTORY_PATH = "tokens";
        private static final String CREDENTIALS_FILE_PATH = "/credentials.json";
        private static List<String> SCOPES = new ArrayList<>();
        private static Classroom service;
        private static String TOPIC_NAME = "projects/temp-260404/topics/temp";
        private static String COURSE_ID = "47737005203";
    
        static {
            SCOPES.addAll(all());
        }
    
        public static void main(String... args) throws IOException, GeneralSecurityException {
    
            final var HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
            service = buildClassroomService(HTTP_TRANSPORT);
    
    //      registerForPushNotifications();
    
            List<Course> courses = getAllCourses();
    
            if (courses == null || courses.size() == 0)
                System.out.println("No courses found.");
            else {
                System.out.println("\nCourses:");
    
                for (var currentCourse : courses)
                    System.out.println(currentCourse.getName() + "(" + currentCourse.getId() + ")");
            }
        }
    
        private static void registerForPushNotifications() throws IOException {
            final var pubSupTopic = new CloudPubsubTopic()
                    .setTopicName(TOPIC_NAME);
    
            final var courseWorkChangesInfo = new CourseRosterChangesInfo()
                    .setCourseId(COURSE_ID);
    
            final var feed = new Feed()
                    .setFeedType("COURSE_WORK_CHANGES")
                    .set("courseWorkChangesInfo", courseWorkChangesInfo);
    
            Registration notificationsRegistration = new Registration()
                    .setFeed(feed)
                    .setCloudPubsubTopic(pubSupTopic);
    
            pubSupTopic.setFactory(JSON_FACTORY);
            courseWorkChangesInfo.setFactory(JSON_FACTORY);
            feed.setFactory(JSON_FACTORY);
            notificationsRegistration.setFactory(JSON_FACTORY);
    
            service.registrations().create(notificationsRegistration).execute();
            System.out.println("Successfully registered");
        }
    
        private static Classroom buildClassroomService(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
            final var serviceCredentials = getCredentials(HTTP_TRANSPORT);
    
            return new Classroom.Builder(HTTP_TRANSPORT, JSON_FACTORY, serviceCredentials)
                    .setApplicationName(APPLICATION_NAME)
                    .build();
        }
    
        private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
            final var clientSecrets = loadJSONClientSecrets();
            final var dataStoreFactory = new FileDataStoreFactory(new File(TOKENS_DIRECTORY_PATH));
    
            final var authenticationFlow = new GoogleAuthorizationCodeFlow.Builder(
                    HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                    .setDataStoreFactory(dataStoreFactory)
                    .setAccessType("offline")
                    .build();
    
            return redirectToAuthentication(authenticationFlow);
        }
    
        private static GoogleClientSecrets loadJSONClientSecrets() throws IOException {
            final var credentialFileStream = getCredentialsJSONFile();
            if (credentialFileStream == null)
                throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
    
            return GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(credentialFileStream));
        }
    
        private static InputStream getCredentialsJSONFile() {
            return TempTest.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
        }
    
        private static Credential redirectToAuthentication(GoogleAuthorizationCodeFlow flow) throws IOException {
            final var receiver = new LocalServerReceiver.Builder().setPort(8888).build();
            return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
        }
    
        private static List<Course> getAllCourses() throws IOException {
            ListCoursesResponse response = service.courses().list()
                    .execute();
    
            return response.getCourses();
        }
    }
    
  1. 转到Google课堂并创建自己的课堂以进行测试。
  2. 转到发布/订阅并创建一个新主题。确保将其设置为“ Google托管密钥”。制作完成后,在“主题名称”下获取其名称。有一个用于复制完整路径的小按钮。
  3. 将类的TOPIC_NAME字段设置为包含您刚复制的主题名称的字符串
  4. 运行代码并在所有范围内进行授权。您将被重定向。确保选择与使用Cloud Platform相同的帐户。
  5. 运行该课程后,您会在括号中找到课程及其ID号的列表。复制您制作的测试课程的ID号。运行代码后,应将其输出到控制台。
  6. 将类的COURSE_ID字段设置为包含刚复制的ID的字符串
  7. 取消注释第40行,然后再次运行代码
  8. 您的示例已完成
  9. 您所做的只是对自己进行身份验证,因此Google知道您所授予的权限,并且可以验证您的身份。然后,您向Google发送了一个JSON请求,其中包含有关要将更新发布到的主题,要获取的更新的类型以及要从中获取这些更新的特定教室的信息。

我强烈建议在这里学习JSON响应的结构。也许以here开头。

This page在Google文档中具有不错的信息。它还显示了您将使用Google Pub / Sub API从另一个程序提取的消息的JSON格式。我没有在这里包括。

谢谢,祝你好运。抱歉,我可能会编辑几次该问题。我现在真的很累。

© www.soinside.com 2019 - 2024. All rights reserved.