making a request to find members of a group in GCP

497 Views Asked by At

this is the request I need to make (trying to get list of members in a google group):

GET https://admin.googleapis.com/admin/directory/v1/groups/{groupKey}/members

https://developers.google.com/admin-sdk/directory/reference/rest/v1/members/list?apix_params=%7B%22groupKey%22%3A%22devops%40flight-analytics.com%22%2C%22includeDerivedMembership%22%3Atrue%2C%22maxResults%22%3A200%2C%22prettyPrint%22%3Atrue%7D

group key in this case is email for the group eg: [email protected] how can i make it into a python script and get my results.

this is the attempt I was expecting to work.

def make_request(request):
    """
    HTTP Cloud Function that makes another HTTP request.
    Args:
        request (flask.Request): The request object.
        <http://flask.pocoo.org/docs/1.0/api/#flask.Request>
    Returns:
        The response text, or any set of values that can be turned into a
        Response object using `make_response`
        <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>.
    """
    import requests

    # The URL to send the request to
    #url = 'https://google.com'
    
    # Process the request
    response = requests.get(request)
    response.raise_for_status()
    print(response)
    print(response.text)
    print(response.raise_for_status())
    print("Success")
    return 'Success!'

make_request("https://admin.googleapis.com/admin/directory/v1/groups/[email protected]/members")

But the script fails saying i dont have proper login but i did ran:

gcloud auth login
gcloud auth application-default login

before firing the python script in same shell but end up getting error:

Traceback (most recent call last):
  File "/Users/deepak.sandhu/Desktop/test.py", line 55, in <module>
    make_request("https://admin.googleapis.com/admin/directory/v1/groups/[email protected]/members")
  File "/Users/deepak.sandhu/Desktop/test.py", line 48, in make_request
    response.raise_for_status()
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/requests/models.py", line 953, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://admin.googleapis.com/admin/directory/v1/groups/[email protected]/members
2

There are 2 best solutions below

4
John Hanley On

gcloud auth login authorizes the CLI (gcloud). Use gcloud auth application-default login to authorize application credentials.

gcloud auth application-default login

0
Amit On

In order to view the members in the group, you need to have atleast the group viewer role. The role is managed from admin console(admin.google.com). Role can be granted by org administrator or group administrator.

I recently did it for my terraform code to view the members in the group and it seems to be working fine.