python-samples
python-samples copied to clipboard
cannot delete all the courses
Expected Behavior
I saved the following script as deleteCourses.py .
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
SCOPES = ['https://www.googleapis.com/auth/classroom.courses']
def main():
creds = None
# 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.
if os.path.exists('token.pickle'):
with open('token.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.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('classroom', 'v1', credentials=creds)
def deleteCourses():
# get the list of all the courses
result = service.courses().list(pageSize=100).execute()
allCourses = result.get('courses', [])
for course in allCourses:
service.courses().delete(id=course['id']).
print("deleted " + course['id'])
deleteCourses()
if __name__ == '__main__':
main()
I hope the script will delete all the courses made in my account.
Actual Behavior
I executed the script and got the error as bellow:
$ python deleteCourses.py
Traceback (most recent call last):
File "deleteCourses.py", line 45, in <module>
main()
File "deleteCourses.py", line 42, in main
deleteCourses()
File "deleteCourses.py", line 39, in deleteCourses
service.courses().delete(id=course['id']).execute()
File "/usr/local/var/pyenv/versions/3.6.10/lib/python3.6/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
return wrapped(*args, **kwargs)
File "/usr/local/var/pyenv/versions/3.6.10/lib/python3.6/site-packages/googleapiclient/http.py", line 907, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://classroom.googleapis.com/v1/courses/93909717095?alt=json returned "Precondition check failed.">
Steps to Reproduce the Problem
- download the
credentials.jsonto the current directory of the Terminal - make the script as above
deleteCourses.pyand executepython deleteCourses.py
Specifications
- Python version
$ python --version
Python 3.6.10
``
- OS
$ sw_vers ProductName: Mac OS X ProductVersion: 10.15.4 BuildVersion: 19E287