Skip to content

Instantly share code, notes, and snippets.

@ochilab
Last active September 17, 2022 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ochilab/40f763ec1924ca896c7c103fe621c7a6 to your computer and use it in GitHub Desktop.
Save ochilab/40f763ec1924ca896c7c103fe621c7a6 to your computer and use it in GitHub Desktop.
Pythonで、Google Classroomの受講生のメールアドレスを取得する
SCOPES = ['https://www.googleapis.com/auth/classroom.courses.readonly'
,'https://www.googleapis.com/auth/classroom.rosters'
,'https://www.googleapis.com/auth/classroom.rosters.readonly'
,"https://www.googleapis.com/auth/classroom.profile.emails",
"https://www.googleapis.com/auth/classroom.profile.photos"]
def getGcStudentList():
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:
pagetoken =""
service = build('classroom', 'v1', credentials=creds)
pageToken=None
while True:
params = {}
params["courseId"] = 00000000 #ここにコースのIDを書く
params["pageSize"] = 50
if pageToken:
params["pageToken"] = pageToken
studentList = service.courses().students().list(**params).execute()
for student in studentList['students']:
print(student['profile']['emailAddress'])
pageToken = studentList.get('nextPageToken')
if not pageToken:
break
except HttpError as error:
print('An error occurred: %s' % error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment