【发布时间】:2017-08-23 16:38:34
【问题描述】:
我正在尝试将 google 课堂集成到我的 java 项目中使用 OAuth 2.0 进行 服务器到服务器 身份验证,此处给出示例 Official Google class room integration doc 使用 OAuth 同意屏幕身份验证,我更改了 Server 到 Server 身份验证的代码,只进行了少量更改
目前的进展:
- 创建了一个谷歌应用
从仪表板启用 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 的详细信息,请告诉我。
提前致谢
【问题讨论】:
标签: google-oauth google-classroom google-oauth-java-client