Skip to content

Instantly share code, notes, and snippets.

@queirozsc
Created May 25, 2020 16:20
Show Gist options
  • Save queirozsc/3751dfe1737533030c8dd6b8b6324ddd to your computer and use it in GitHub Desktop.
Save queirozsc/3751dfe1737533030c8dd6b8b6324ddd to your computer and use it in GitHub Desktop.
Google Classroom API
import os
import pickle
from google.auth.transport.requests import Request # pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from django.db import models
from django.conf import settings
# Create your models here.
class Greeting(models.Model):
when = models.DateTimeField("date created", auto_now_add=True)
class Classroom(models.Model):
service = None
def get_token(self):
""" The file token.pickle stores the user's access and refresh tokens,
and is created automatically when the authorization flow completes
for the first time"""
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
return creds
def save_token(self, credentials):
"""Save the credentials for next run"""
with open('token.pickle', 'wb') as token:
pickle.dump(credentials, token)
def login(self):
"""If there are no (valid) credentials available, let the user log in"""
creds = self.get_token()
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', settings.GOOGLE_API_SCOPES)
creds = flow.run_local_server(port=0)
self.save_token(credentials=creds)
self.service = build('classroom', 'v1', credentials=creds)
def list_courses(self):
results = self.service.courses().list().execute()
return results.get('courses', [])
class Course(models.Model):
id = models.CharField(max_length=12)
name = models.CharField('Turma', max_length=60)
def __str__(self):
return self.name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment