Google Classroom API

Hey @dustin or @sandman I dunno if you have any experience with Google APIs and Python.

I’m trying to create an “announcement” in a Google classroom with the Google Classroom API.

So far, I’ve successfully generated the authorizations, enabled the appropriate scope for those authorizations, and I’m able to do simple things like list all the Google classrooms in my account. So I know the authorizations are good, and I can actually connect.

However, I’m stuck at this simple thing.

https://developers.google.com/classroom/reference/rest/v1/courses.announcements?authuser=1#Announcement

I’m able to post text just fine with the API explorer in the browser, but not in Python.

What would I do to actually post an announcement in Python?

so far…

from __future__ import print_function
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

SCOPES = 'https://www.googleapis.com/auth/classroom.announcements'

def main():
    store = file.Storage('token.json')
    creds = store.get()
    service = build('classroom', 'v1', http=creds.authorize(Http()))
    Announcement = {
        "text": "This is a test",
    }

    service.courses.announcements.create(courseId=24530521838, text=Announcement).execute()



if __name__ == '__main__':
    main()

But I get an error `AttributeError: ‘function’ object has no attribute ‘announcements’

So I’m sure I’m messing something up. Any ideas?

Take a look at the example here:

https://developers.google.com/classroom/guides/manage-coursework

courseWork = {  
  'title': 'Ant colonies',  
  'description': 'Read the article about ant colonies and complete the quiz.',  
  'materials': [  
     {'link': { 'url': 'http://example.com/ant-colonies' }},  
     {'link': { 'url': 'http://example.com/ant-quiz' }}  
],  
  'workType': 'ASSIGNMENT',  
  'state': 'PUBLISHED',  
}  
courseWork = service.courses().courseWork().create(  
    courseId='<course ID or alias>', body=courseWork).execute()  
print('Assignment created with ID {0}'.format(courseWork.get('id')))

it’s for courseWork(), but it should work the same way for announcements?

Thanks! That seems to have done the trick. I just replaced “courseWork” with “announcement” and it’s working great.

1 Like

@miguel are you the one who is doing this for mister Bowie.

1 Like