0

我正在尝试将谷歌教室集成到我的 java 项目中使用 OAuth 2.0 进行服务器到服务器身份验证,此处给出了示例官方 Google 教室集成文档使用OAuth 同意屏幕身份验证,我更改了服务器到服务器身份验证的代码,并进行了一些更改

目前进展:

  • 创建了一个谷歌应用
  • 从仪表板启用 Google 课堂 API。

  • 为服务器到服务器身份验证创建服务帐户密钥 (.p12)(后来在代码中与应用程序名称一起使用)

    public class Quickstart2 {
        private static final String APPLICATION_NAME ="MY_APP_NAME";
        private static final JsonFactory JSON_FACTORY =
            JacksonFactory.getDefaultInstance();
        private static HttpTransport httpTransport;
    
        /** Global instance of the HTTP transport. */
        private static HttpTransport HTTP_TRANSPORT;
        private static final String SERVICE_ACCOUNT_EMAIL = "ID_CREADED_ON_GOOGLE_APP";
        /** Global instance of the scopes required by this quickstart.
         *
         * If modifying these scopes, delete your previously saved credentials
         * at ~/.credentials/classroom.googleapis.com-java-quickstart
         */
        private static final List<String> SCOPES =
            Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_ME);
    
        static {
            try {
                HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
            } catch (Throwable t) {
                t.printStackTrace();
                System.exit(1);
            }
        }
    
        /**
         * Creates an authorized Credential object.
         * @return an authorized Credential object.
         * @throws IOException
         * @throws GeneralSecurityException 
         */
        public static Credential authorize() throws IOException, GeneralSecurityException {  
            /* Added this code in given sample code for Server-to-server authentication*/
    
    
          httpTransport = GoogleNetHttpTransport.newTrustedTransport();
          GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
              .setJsonFactory(JSON_FACTORY)
              .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
              .setServiceAccountScopes(SCOPES)
              .setServiceAccountPrivateKeyFromP12File(new File("BULB-1cb9e145138b.p12"))
               .setServiceAccountUser("bulb-88@bulb-173512.iam.gserviceaccount.com") 
              .build();
          return credential;
        }
    
        /**
         * Build and return an authorized Classroom client service.
         * @return an authorized Classroom client service
         * @throws IOException
         * @throws GeneralSecurityException 
         */
        public static com.google.api.services.classroom.Classroom
                getClassroomService() throws IOException, GeneralSecurityException {
            Credential credential = authorize();
            System.out.println("token   "+credential.getAccessToken());
            return new com.google.api.services.classroom.Classroom.Builder(
                    HTTP_TRANSPORT, JSON_FACTORY, credential)
                    .setApplicationName(APPLICATION_NAME)
                    .build();
        }
    
        public static void main(String[] args) throws IOException, GeneralSecurityException {
            // Build a new authorized API client service.
            com.google.api.services.classroom.Classroom service =
                getClassroomService();
    
            Course course = new Course()
            .setName("10th Grade Biology")
            .setSection("Period 2")
            .setDescriptionHeading("Welcome to 10th Grade Biology")
            .setDescription("test descriptioon")
            .setRoom("301")
            .setOwnerId("me")
            .setCourseState("PROVISIONED");
             course = service.courses().create(course).execute(); /* here I am getting the exception */
    
    
           //  List the first 10 courses that the user has access to.
    
            ListCoursesResponse response = service.courses().list()
                .setPageSize(10)
                .execute();
            List<Course> courses = response.getCourses();
            System.out.println(courses);
            if (courses == null || courses.size() == 0) {
                System.out.println("No courses found.");
            } else {
                System.out.println("Courses:");
                for (Course course1 : courses) {
                    System.out.printf("%s\n", course1.getName());
                }
            }
        }
    

    }

我相信身份验证没有任何问题,因为每当我运行这段代码时,都会在我的应用程序“ https://console.developers.google.com/apis/dashboard ”的仪表板中获得一个条目

这是我在仪表板上得到的图像

我是谷歌教室的新手,很长时间以来我一直在尝试整合这个到目前为止没有成功,需要帮助。

还有一件事它说必须拥有一个G套件帐户(我不知道它在哪里使用,但我还没有创建G套件帐户,这就是我得到例外的原因)

如果我想在上面的 senerio 上添加更多详细信息,请告诉我。

提前致谢

4

1 回答 1

0

Gsuite 帐户可在此处找到。课堂 API 范围可在使用 OAuth 2.0 授权请求中找到

于 2017-08-24T10:38:49.890 回答