-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_classroom_credential.py
More file actions
107 lines (98 loc) · 4.69 KB
/
Copy pathgenerate_classroom_credential.py
File metadata and controls
107 lines (98 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# https://developers.google.com/classroom/quickstart/python
# pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
# inputs: none
# output: service object for Google classroom
# def generate_classroom_credential():
# import pickle
# import os.path
# from googleapiclient.discovery import build
# from google_auth_oauthlib.flow import InstalledAppFlow
# from google.auth.transport.requests import Request
# import httplib2
#
# scopes = ['https://www.googleapis.com/auth/classroom.announcements',
# 'https://www.googleapis.com/auth/classroom.courses',
# 'https://www.googleapis.com/auth/classroom.coursework.students',
# 'https://www.googleapis.com/auth/classroom.rosters',
# 'https://www.googleapis.com/auth/classroom.topics',
# 'https://www.googleapis.com/auth/classroom.courseworkmaterials',
# 'https://www.googleapis.com/auth/classroom.profile.emails',
# 'https://www.googleapis.com/auth/classroom.guardianlinks.students.readonly',
# ]
#
#
# creds = None
# # The file token_classroom.pickle stores the user's access and refresh tokens, and is
# # created automatically when the authorization flow completes for the first
# # time.
# if os.path.exists('token_classroom.pickle'):
# with open('token_classroom.pickle', 'rb') as token:
# creds = pickle.load(token)
# # If there are no (valid) credentials available, let the user log in.
# 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_classroom.json', scopes)
# creds = flow.run_local_server()
# # Save the credentials for the next run
# with open('token_classroom.pickle', 'wb') as token:
# pickle.dump(creds, token)
# try:
# service = build('classroom', 'v1', credentials=creds)
# except httplib2.ServerNotFoundError:
# raise Exception("Could not reach Google's servers to create a Google CLASSROOM service object. Internet down?"
# "Google down? Your DNS not working?")
# print("Google classroom service object generated")
# return service
#
def generate_classroom_credential():
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
scopes = ['https://www.googleapis.com/auth/classroom.announcements',
'https://www.googleapis.com/auth/classroom.courses',
'https://www.googleapis.com/auth/classroom.coursework.students',
'https://www.googleapis.com/auth/classroom.rosters',
'https://www.googleapis.com/auth/classroom.topics',
'https://www.googleapis.com/auth/classroom.courseworkmaterials',
'https://www.googleapis.com/auth/classroom.profile.emails',
'https://www.googleapis.com/auth/classroom.guardianlinks.students.readonly',
]
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists("token_classroom.json"):
creds = Credentials.from_authorized_user_file("token_classroom.json", scopes)
# If there are no (valid) credentials available, let the user log in.
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_classroom.json", scopes
)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open("token_classroom.json", "w") as token:
token.write(creds.to_json())
try:
service = build("classroom", "v1", credentials=creds)
# # Call the Classroom API
# results = service.courses().list(pageSize=10).execute()
# courses = results.get("courses", [])
#
# if not courses:
# print("No courses found.")
# return
# # Prints the names of the first 10 courses.
# print("Courses:")
# for course in courses:
# print(course["name"])
return service
except HttpError as error:
print(f"An error occurred: {error}") # and then crashes