【问题标题】:Google Admin SDK: Get a list of groups that a user belongs toGoogle Admin SDK:获取用户所属组的列表
【发布时间】:2014-07-31 23:33:24
【问题描述】:

我发现可以列出属于特定组的所有成员,如此处所述:

https://developers.google.com/admin-sdk/directory/v1/guides/manage-group-members#get_all_members

但是是否可以获得用户所属组的列表?我希望 Users.get 会包含这些信息,或者 Members API 会有类似的东西,但我没有看到。

【问题讨论】:

    标签: google-admin-sdk


    【解决方案1】:

    实现这一点的最佳方法是使用 google admin sdk api。

    groups.list()

    groups.list() with details

    例如,如果你想使用 python sdk 来做,你可以使用以下

    from __future__ import print_function
    import logging
    import os.path
    import csv
    import json
    from googleapiclient.discovery import build
    from google_auth_oauthlib.flow import InstalledAppFlow
    from google.auth.transport.requests import Request
    from google.oauth2.credentials import Credentials
    
    # If modifying these scopes, delete the file token.json.
    ### https://developers.google.com/admin-sdk/directory/v1/guides/authorizing
    ### https://developers.google.com/admin-sdk/directory/v1/quickstart/python
    SCOPES = ['https://www.googleapis.com/auth/admin.directory.user',
              'https://www.googleapis.com/auth/admin.directory.group']
    
    
    """
    ########################################################################################################################################################
    # Logging level set for the script
    # https://realpython.com/python-logging/
    ########################################################################################################################################################
    """
    logging.basicConfig(level=logging.INFO)
    
    class GSuite_management(object):
      """
      ########################################################################################################################################################
      # GSuite_management CLASS
      #  --> This class will have methods to manage the memebers of organization using Gsuite Admin SDK
      ########################################################################################################################################################
      """
    
      service = None
    
      def __init__(self):
          """
          GSuite_management Constrouctor
          """
          creds = None
          # The file token.json stores the user's access and refresh tokens, and is
          # created automatically when the authorization flow completes for the first
          # time.
          if os.path.exists('token.json'):
              creds = Credentials.from_authorized_user_file('token.json', SCOPES)
          # If there are no (valid) credentials available, let the user log in.
          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(
                      'credentials.json', SCOPES)
                  creds = flow.run_local_server(port=0)
              # Save the credentials for the next run
              with open('token.json', 'w') as token:
                  token.write(creds.to_json())  
          self.service = build('admin', 'directory_v1', credentials=creds)          
    
      def list_all_groups_a_user_is_a_part_of(self, userEmail):
        """
        This method will list all the groups a user is a part of and return them as a list
        """
        listOfEmailGroups=[]
        try:
          results = self.service.groups().list(domain="yourdomain.com",userKey=userEmail, maxResults=400).execute()
          logging.debug(results)
          groups = results.get('groups', [])
          if not groups:
              print('No groups in the domain.')
          else:
              for group in groups:
                logging.info(u'{0} {1} {2}'.format(group['email'],group['name'], group['directMembersCount']))
                listOfEmailGroups.append(group['email'])
        except Exception as e:
          logging.error("Exiting!!!")
          SystemExit(e) 
        return listOfEmailGroups
    

    【讨论】:

      【解决方案2】:

      所以我在开发人员指南中找到了解决方案,尽管它在 API 文档中不存在!!

      https://developers.google.com/admin-sdk/directory/v1/guides/manage-groups#get_all_member_groups

      【讨论】:

        猜你喜欢
        • 2020-03-05
        • 1970-01-01
        • 2013-07-10
        • 2014-12-20
        • 2019-02-08
        • 1970-01-01
        • 2016-12-11
        • 2012-07-21
        相关资源
        最近更新 更多