How to quick start with Google Classroom API

Francislainy Campos
HMH Engineering
Published in
5 min readNov 2, 2021

We have been working with the Google Classroom APIs and would like to share a bit of our findings on how to use the Google Console and set up a quick start project so you can move forward from there. This will be a summary based on the quickStart java project and the classroom docs and will assume we have a Google classroom account created and set up with teachers and courses.

Setup for the Java repo

Set up a Java project (below example is done using Gradle and following the quickstart page)

apply plugin: 'java'
apply plugin: 'application'

version = '1.0'

repositories {
mavenCentral()
}

dependencies {
implementation 'org.projectlombok:lombok:1.18.18'
compile 'com.google.api-client:google-api-client:1.23.0'
compile 'com.google.oauth-client:google-oauth-client-jetty:1.23.0'
compile 'com.google.apis:google-api-services-classroom:v1-rev135-1.23.0'
}

If you prefer maven, this is the equivalent for the gradle piece above.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>gcClassroomMaven</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>

<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.23.0</version>
</dependency>

<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.23.0</version>
</dependency>

<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-classroom</artifactId>
<version>v1-rev135-1.23.0</version>
</dependency>

</dependencies>

</project>

And here our ClassroomQuickStart class which we’re placing under classroom/quickstart/src/main/java/ClassroomQuickStart

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.classroom.ClassroomScopes;
import com.google.api.services.classroom.model.*;
import com.google.api.services.classroom.Classroom;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.List;

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";

/**
* Global instance of the scopes required by this quickstart.
* If modifying these scopes, delete your previously saved tokens/ folder.
*/
private static final List<String> SCOPES = Collections.singletonList(ClassroomScopes.CLASSROOM_COURSES_READONLY);
private static final String CREDENTIALS_FILE_PATH = "/credentials.json";

/**
* Creates an authorized Credential object.
* @param HTTP_TRANSPORT The network HTTP Transport.
* @return An authorized Credential object.
* @throws IOException If the credentials.json file cannot be found.
*/
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());
}
}
}
}

Google Cloud Console

Create project

Now under the Google Classroom cloud console the next step is to create a project and retrieve its credentials.json file at the end of the process.

Click arrow to open select project wizard and click New Project button
Type project name
Select newly created project from list

APIs and Services

Once the project is created, navigate to the APIs and Services screen.

Open drawer and click APIs and Services

Configure consent

Configure consent screen

Click Configure Consent Screen
Select user type on Oauth consent screen
Add app name
Add developer contact

Scopes

Add or remove permission scopes (this can also be done programatically later on)

Click add or remove scopes
Select scope checkboxes

Test users

As our app is not yet verified, we need to manually add test users, as those will be the ones who have access to our application.

Click add users button

Retrieve Credentials.json file

Click create credentials.

Click Create Credentials button
Click Oauth client ID dropdown item

Select the type of application you are building.

Select application type from dropdown

Download the json file which holds the credentials for the application

Click Download json

Back to the Java repo

Rename it to credentials.json and add it to your applications’ resources folder.

Credentials.json file pasted within resources folder

Running the project and verifying it through the browser

Now if you run the project you should be able to see a Google authentication screen where the user is asked to accept the permissions the application requires (scopes).

Click continue on Google verification screen
Select scopes view

Once these are accepted you should be redirected to a screen similar to the below.

The list of courses your user in entitled to see is now printed under the terminal window.

Terminal printing list of courses

And a tokens folder will be generated under your project tree. (if you would like to see the google accept scopes again and restart the flow, you can do this by deleting the tokens folder).

Autogenerated tokens folder and StoredCredential file

Thank you for reading this article and I hope you found it useful.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in HMH Engineering

HMH Engineering builds fantastic software to meet the challenges facing teachers and learners. We enable and support a wide range of next-generation learning experiences, designing and building apps and services used daily by millions of students and educators across the USA.

Written by Francislainy Campos

I like coding, cycling, K-Pop (girl groups), Pokémon and chocolate.

No responses yet

What are your thoughts?