【问题标题】:"The caller does not have permission", when adding teacher to a course through the Google Classroom API通过 Google Classroom API 将教师添加到课程时,“调用者没有权限”
【发布时间】: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


【解决方案1】:

如果我没记错的话,请求用户必须是域管理员或超级管理员才能创建教师。

非超级管理员用户只能访问他们所属的课程(作为教师或学生),而不是域中的所有课程。

他们可以通过 course.teachers.delete 和 course.students.delete 直接从他们拥有的课程中删除学生和其他教师,但他们不能通过 course.teachers.create 和 course.students 直接将新学生和教师添加到他们的课程中。创造。只有域管理员(超级管理员)才能做到这一点。非管理员必须先通过invitations.create() 发送邀请,并征得用户同意。

虽然您应该能够使用服务帐号来冒充超级管理员。样品可以找到here

参考:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-31
    • 2020-11-11
    • 2022-07-13
    • 2021-02-21
    相关资源
    最近更新 更多