Classroom API - CourseAlias

0

The following code refers to creating a course through the Classroom API. However the API does not allow you to manually set an 'ID' (using .setId ("ID")). For this, it is possible to create an Alias (single) for each course.

public void createCourse() throws 
{
    Course course = new Course()
            .setName(name)
            .setSection(alias+" - "+periodo.toUpperCase())
            .setDescriptionHeading(heading)
            .setDescription(desc)
            .setOwnerId(professor)
            .setEnrollmentCode(alias.toLowerCase())
            .setCourseState("ACTIVE");
    course = service.courses().create(course).execute();
    System.out.printf("Course created: %s (%s)\n", course.getName(), course.getId());

    CourseAlias courseAlias = new CourseAlias()
            .setAlias("Teste");
    courseAlias = service.courses().aliases().create(course.getId(), courseAlias)
            .execute();
    System.out.printf("Course alias: %s (%s)\n", "10820582852", courseAlias.getAlias());
}

The course is created without problems, but when the request to create the alias returns the following error.

Exception in thread "main" 
com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad 
Request
{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "message" : "Request contains an invalid argument.",
    "reason" : "badRequest"
  } ],
  "message" : "Request contains an invalid argument.",
  "status" : "INVALID_ARGUMENT"
}
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
    at management.Cursos.createCourse(Cursos.java:64)
    at client.LoadClassroom.main(LoadClassroom.java:108)
    
asked by anonymous 24.01.2018 / 18:09

1 answer

0

Solved! The alias string has its own configuration, the string must start with 'd:' which indicates domain scope or 'p:' that indicates project scope.

I used to look like this:

CourseAlias courseAlias = new CourseAlias()
        .setAlias("Teste");

but the correct one is:

CourseAlias courseAlias = new CourseAlias()
        .setAlias("d:Teste");
    
31.01.2018 / 22:18