Search code examples
python-3.xapigoogle-classroom

Get student email from userID


I have been able to get the courses and studentsubmission lists from classroom successfully.

I now want to get email address for students rather than use the userID that studentsubmission returns.
https://developers.google.com/classroom/guides/manage-users very usefully says use userProfiles.get so using this line =

studentEmail = service.userProfiles().get(userId = student['userId'])

which results in the following output:

<googleapiclient.http.HttpRequest object at 0x0000000003F9C048> TURNED_IN

As expected I have 1 student in the test classroom and 1 student who has finished the work. But that is clearly not an email address! So what do I need to do please with my get()? I have tried

get().getEmailaddress()

But that doesn't seem to work and docs are a little vague for me.

Thanks in advance.

EDIT ***

made a couple of changes so my loop now looks like this students = studentSubmissionResults.get('studentSubmissions', [])

for student in students:
    studentId = service.userProfiles().get(userId = 
                student['userId']).execute()
    studentEmail = studentId.get('emailAddress').execute()
    assignmentState = student['state']
    print('The following students have completed the work:\n', studentEmail 
           , assignmentState )

this has resulted in a insufficient scope error, but I have all the scopes suggested on the userProfile.get() page ? ? ? ?


Solution

  • From the docs, it appears you should use

    .get('emailAddress')
    

    on the result of the userProfiles.get(userId) call.

    In addition, you should wrap this call in a try-except block to handle failures in your request.