|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from __future__ import print_function |
|
|
|
import os.path |
|
|
|
from google.auth.transport.requests import Request |
|
from google.oauth2.credentials import Credentials |
|
from googleapiclient.discovery import build |
|
from googleapiclient.errors import HttpError |
|
|
|
|
|
SCOPES = ['https://www.googleapis.com/auth/classroom.courses.readonly'] |
|
|
|
|
|
def main(): |
|
"""Shows basic usage of the Classroom API. |
|
Prints the names of the first 10 courses the user has access to. |
|
""" |
|
creds = None |
|
|
|
|
|
|
|
if os.path.exists('token.json'): |
|
creds = Credentials.from_authorized_user_file('token.json', SCOPES) |
|
|
|
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', SCOPES) |
|
creds = flow.run_local_server(port=0) |
|
|
|
with open('token.json', 'w') as token: |
|
token.write(creds.to_json()) |
|
|
|
try: |
|
service = build('classroom', 'v1', credentials=creds) |
|
|
|
|
|
results = service.courses().list(pageSize=10).execute() |
|
courses = results.get('courses', []) |
|
|
|
if not courses: |
|
print('No courses found.') |
|
return |
|
|
|
print('Courses:') |
|
for course in courses: |
|
print(course['name']) |
|
|
|
except HttpError as error: |
|
print('An error occurred: %s' % error) |
|
|
|
|
|
if __name__ == '__main__': |
|
main() |
|
|