googleworkspace / python-samples

🐍 Python samples for Google Workspace APIs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://cloud.google.com/docs/authentication/getting-started

victor-rayan opened this issue · comments

Summary

Expected Behavior

a list of course work that the requester is permitted to view.

{
"courseWork": [
{
object (CourseWork)
}
],
"nextPageToken": string
}

Actual Behavior

Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://cloud.google.com/docs/authentication/getting-started

Steps to Reproduce the Problem

SCOPES = [
    'https://www.googleapis.com/auth/classroom.coursework.students.readonly',
    'https://www.googleapis.com/auth/classroom.coursework.me.readonly',
    'https://www.googleapis.com/auth/classroom.coursework.students',
    'https://www.googleapis.com/auth/classroom.coursework.me'
]


def listcourseWork(idcourse):
   
    creds = None
    try:
        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(
                    'client_secret.json', SCOPES)
                creds = flow.run_local_server(port=0)
                print(creds)
            # Save the credentials for the next run
            try:
                with open('token.json', 'w') as token:
                    token.write(creds.to_json())
            except Exception as error:
                print(error)       
    except Exception as error:
        print(error)          
    

    try:
        service = build('classroom', 'v1', credentials=creds)
        studentsFormated = []
        # Call the Classroom API
        results = service.courses().courseWork().list(courseId = idcourse).execute()
        print(results)

    except Exception as error:
        print(error)    
    
        
if __name__ == '__main__':
    listcourseWork('525987525315')

Specifications

  • Python version (`python --3.10)
  • OS (Windows)

SOLVED.

SCOPES = [
'https://www.googleapis.com/auth/classroom.coursework.students'
]