【发布时间】:2021-10-13 08:36:10
【问题描述】:
我正在尝试通过 google 课堂 API 为学生创建作业。但是,我了解到管理员不能这样做,只有教师才能做到。因此,在我们的代码中,在创建新课程后,我们将设置教师的电子邮件,这与管理员的电子邮件相同,并将其添加到 .但是,尝试将教师添加到课程时,代码会失败。它说“来电者没有权限”。我不确定需要什么额外的权限,并且一直停留在此错误消息上。
private static final List<String> SCOPES =
Arrays.asList(ClassroomScopes.CLASSROOM_COURSES,
ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS,
ClassroomScopes.CLASSROOM_COURSEWORK_ME,
ClassroomScopes.CLASSROOM_PROFILE_EMAILS,
ClassroomScopes.CLASSROOM_PROFILE_PHOTOS,
ClassroomScopes.CLASSROOM_ROSTERS);
Course course = new Course()
.setName("10th Grade Biology")
.setSection("Period 2")
.setDescriptionHeading("Welcome to 10th Grade Biology")
.setDescription("We'll be learning about about the structure of living creatures "
+ "from a combination of textbooks, guest lectures, and lab work. Expect "
+ "to be excited!")
.setRoom("301")
.setOwnerId("me")
.setCourseState("PROVISIONED");
course = service.courses().create(course).execute();
System.out.printf("Course created: %s (%s)\n", course.getName(), course.getId());
String courseId = course.getId();
String teacherEmail = "admin@gmail.com";
Teacher teacher = new Teacher().setUserId(teacherEmail);
try {
teacher = service.courses().teachers().create(courseId, teacher).execute();
System.out.printf("User '%s' was added as a teacher to the course with ID '%s'.\n",
teacher.getProfile().getName().getFullName(), courseId);
} catch (GoogleJsonResponseException e) {
GoogleJsonError error = e.getDetails();
if (error.getCode() == 409) {
System.out.printf("User '%s' is already a member of this course.\n", teacherEmail);
} else {
throw e;
}
}
public class ClassroomQuickstart {
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 List<String> SCOPES =
Arrays.asList(ClassroomScopes.CLASSROOM_COURSES,
ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS,
ClassroomScopes.CLASSROOM_COURSEWORK_ME,
ClassroomScopes.CLASSROOM_ROSTERS);
private static final String CREDENTIALS_FILE_PATH = "/credentials.json";
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
// Load client secrets.
InputStream in = ClassroomQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
if (in == null) {
throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
}
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline")
.build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
public static void main(String... args) throws IOException,
GeneralSecurityException {
// Build a new authorized API client service.
final NetHttpTransport HTTP_TRANSPORT =
GoogleNetHttpTransport.newTrustedTransport();
Classroom service = new Classroom.Builder(HTTP_TRANSPORT, JSON_FACTORY,
getCredentials(HTTP_TRANSPORT))
.setApplicationName(APPLICATION_NAME)
.build();
// List the first 10 courses that the user has access to.
ListCoursesResponse response = service.courses().list()
.setPageSize(10)
.execute();
List<Course> courses = response.getCourses();
if (courses == null || courses.size() == 0) {
System.out.println("No courses found.");
} else {
System.out.println("Courses:");
for (Course course : courses) {
System.out.printf("%s\n", course.getName());
}
}
Course course = new Course()
.setName("10th Grade Biology")
.setSection("Period 2")
.setDescriptionHeading("Welcome to 10th Grade Biology")
.setDescription("We'll be learning about about the structure of living creatures "
+ "from a combination of textbooks, guest lectures, and lab work. Expect "
+ "to be excited!")
.setRoom("301")
.setOwnerId("me")
.setCourseState("PROVISIONED");
course = service.courses().create(course).execute();
System.out.printf("Course created: %s (%s)\n", course.getName(), course.getId());
String courseId = course.getId();
String teacherEmail = "admin@gmail.com";
Teacher teacher = new Teacher().setUserId(teacherEmail);
try {
teacher = service.courses().teachers().create(courseId, teacher).execute();
System.out.printf("User '%s' was added as a teacher to the course with ID '%s'.\n",
teacher.getProfile().getName().getFullName(), courseId);
} catch (GoogleJsonResponseException e) {
GoogleJsonError error = e.getDetails();
if (error.getCode() == 409) {
System.out.printf("User '%s' is already a member of this course.\n", teacherEmail);
} else {
throw e;
}
}
// CourseWork courseWork = new CourseWork()
// .setCourseId(course.getId())
// .setTitle("title")
// .setWorkType("ASSIGNMENT")
// .setDescription("desc");
// service.courses().courseWork().create(course.getId(), courseWork).execute();
}
}
谢谢。
【问题讨论】:
-
您是否重新考虑过使用域管理员或通过服务帐户模拟它?以下答案中的相关链接。
标签: google-api google-oauth google-classroom