pythonpython-3.xgoogle-apigoogle-admin-sdkgoogle-groups-api

Google Api + List members of a group with python


Well, basicaly I'm lost with this task. I need to get all members of a group with the api google privedes. I'm starting with gmail quickstart example, first I have configure Api project and the OAuth client.

My first test was to see all the messages I have in my inbox, all ok. But to do the same with groups members I'm lost, I can't find the way to do it.

Any idea?

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

SCOPES = 'https://www.googleapis.com/auth/admin.directory.group.member'

def main():
    store = file.Storage('token.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
        creds = tools.run_flow(flow, store)
    service = build('gmail', 'v1', http=creds.authorize(Http()))

    response = requests.get("https://www.googleapis.com/admin/directory/v1/groups/example@test.com/members")
    print(response)

The response is a:

<Response [401]>

Solution

  • Looks like you started with the gmail example and then modified to access the admin/group function

    Check the admin docs here https://developers.google.com/admin-sdk/directory/v1/quickstart/python