Classroom API - Coursealias

Asked

Viewed 102 times

0

The following code refers to the creation of a course by the Classroom API. However, the API does not allow you to manually define an 'ID' (using .setId("ID")). For this is available the creation of 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 me 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)
  • which url you are sending the request?

  • The . execute() command works with the urls needed to make the requests... I did basically as shown in API, I just changed the scope and some access settings.

  • The necessary scope for this operation is: https://www.googleapis.com/auth/classroom.courses

1 answer

0


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

Before I had this:

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

but the right thing is:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.