Integrate Bolt.new AI with Google Classroom in minutes with our step-by-step guide to boost teaching efficiency, streamline grading, and engage students.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
package.json
file.googleapis
as a dependency by including the following in the "dependencies" section:
"dependencies": {
"googleapis": "^105.0.0"
// ...other dependencies
}
package.json
will automatically include the dependency on deployment.
googleClassroom.ts
in your project’s root directory.googleClassroom.ts
. This code sets up an OAuth2 client, generates an authentication URL, handles the callback token exchange, and includes an example method for listing courses from Google Classroom:
import { google } from 'googleapis';
const CLIENTID = process.env.GOOGLECLIENTID || 'YOURCLIENT_ID';
const CLIENTSECRET = process.env.GOOGLECLIENTSECRET || 'YOURCLIENT_SECRET';
const REDIRECTURI = process.env.GOOGLEREDIRECTURI || 'YOURREDIRECT_URI';
export class GoogleClassroomService {
private oauth2Client = new google.auth.OAuth2(
CLIENT_ID,
CLIENT_SECRET,
REDIRECT_URI
);
// Generates a URL to direct users to Google for Classroom authorization
getAuthUrl(): string {
const scopes = ['https://www.googleapis.com/auth/classroom.courses.readonly'];
const url = this.oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: scopes,
});
return url;
}
// Exchanges the provided code for access tokens
setCredentials(code: string): Promise {
return new Promise((resolve, reject) => {
this.oauth2Client.getToken(code, (err, tokens) => {
if (err) {
return reject(err);
}
this.oauth2Client.setCredentials(tokens || {});
resolve();
});
});
}
// Example method: Lists Google Classroom courses
async listCourses() {
const classroom = google.classroom({ version: 'v1', auth: this.oauth2Client });
const res = await classroom.courses.list();
return res.data.courses;
}
}
index.ts
or app.ts
), import the Express framework and the GoogleClassroomService
you created.
import express from 'express';
import { GoogleClassroomService } from './googleClassroom';
const app = express();
const port = process.env.PORT || 3000;
const googleService = new GoogleClassroomService();
// Route to redirect users to Google for Classroom permissions
app.get('/auth/google', (req, res) => {
const authUrl = googleService.getAuthUrl();
res.redirect(authUrl);
});
// Google OAuth2 callback route
app.get('/auth/google/callback', async (req, res) => {
const code = req.query.code as string;
try {
await googleService.setCredentials(code);
const courses = await googleService.listCourses();
res.send(courses);
} catch (error) {
res.status(500).send('Error during Google authentication');
}
});
app.listen(port, () => {
console.log(Server listening on port ${port});
});
/auth/google
) and another to handle the OAuth2 callback (/auth/google/callback
).
GOOGLECLIENTID
: Your Google API client IDGOOGLECLIENTSECRET
: Your Google API client secretGOOGLEREDIRECTURI
: The redirect URI configured in your Google API Console (typically something like https://your-app-domain/auth/google/callback
)process.env
.
/auth/google
endpoint in your deployed app. You should be redirected to Google’s login page for Classroom authorization./auth/google/callback
, where the app exchanges the code for tokens and displays your Google Classroom courses (if any).
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.