我正在尝试通过谷歌课堂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();
}
}谢谢。
发布于 2021-10-13 19:54:00
如果我没记错的话,请求用户必须是域管理员或超级管理员才能创建教师。
非超级管理员用户只能访问他们所属的课程(作为教师或学生),而不是域中的所有课程。他们可以通过courses.teachers.delete和courses.students.delete将学生和其他教师从他们直接拥有的课程中删除,但不能通过courses.teachers.create和courses.students.create直接将新学生和教师添加到他们的课程中。只有域管理员(超级管理员)才能执行此操作。非管理员必须首先通过invitations.create()发送邀请,并获得用户的同意。
尽管您应该能够通过使用服务帐户来模拟超级管理员。样本可以在here上找到
参考资料:
https://stackoverflow.com/questions/69552255
复制相似问题