Python SDK,正在尝试呼叫Gmail组的成员并更新

Python SDK,正在尝试呼叫Gmail组的成员并更新,python,member,google-admin-sdk,Python,Member,Google Admin Sdk,我正在尝试创建一个电话,获取所有Gmail群电子邮件,这样我就可以更新那些不存在的邮件,删除那些不应该存在的邮件。我目前正在尝试下面的代码,我得到一个范围错误 # If modifying these scopes, delete the file token.json. SCOPES = ['https://www.googleapis.com/auth/admin.directory.group.members', 'https://www.googleapis.com/auth/admin

我正在尝试创建一个电话,获取所有Gmail群电子邮件,这样我就可以更新那些不存在的邮件,删除那些不应该存在的邮件。我目前正在尝试下面的代码,我得到一个范围错误

# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/admin.directory.group.members', 'https://www.googleapis.com/auth/admin.directory.group']

def main():
    """Shows basic usage of the Admin SDK Directory API.
    Prints the emails and names of the first 10 users in the domain.
    """
    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())

    service = build('admin', 'directory_v1', credentials=creds)

 # Call the Admin SDK Directory API
    print('Getting the members of Hospitality Team')
    response_group = service.groups().list(customer='my_customer').execute()
    for group in response_group['groups']:
       print(group['email'])
解决方案: 您可以执行以下操作:

  • 通过列出您的所有群组
  • 检查每个组是否有成员
  • 如果该组有成员,请通过检索其成员
  • 对于来自其他API的每个所需成员,请检查其是否已存在于组中。如果不存在,请通过将其添加到您的群中
  • 对于组中的每个当前成员,检查其是否为所需成员之一。如果不是,请通过删除它
  • 如果组中没有成员,请通过
    members:insert
    将所有所需成员添加到组中
代码段: 笔记:
  • 您提供的作用域应该足以进行这些调用。如果您在上次身份验证后编辑了这些作用域,请删除旧的
    token.json
    ,然后再次进行身份验证
  • 确保经过身份验证的用户具有对这些组的编辑权限
  • 在这里,我假设所有组所需的成员列表都是相同的。我还假设您有这些电子邮件的列表(当前
    ideal\u会员\u电子邮件
    )。如果不是这样,请根据您的偏好编辑提供的脚本
  • 如果您的组和成员列表足够大,您应该迭代地获取
    列表
    请求的不同页面结果。有关如何执行此操作的更多信息,请参见此部分(关于
    用户:列表,但过程相同)
参考:

您从哪里获得此代码?您正在尝试通过
构建
进行身份验证,然后尝试通过
请求
执行请求。您应该使用构建的
服务
来调用API。请先看一下,然后修改请求部分,改为呼叫。我想提供一个更详细的答案来解释这一点,但首先我想澄清您想要做什么确切的
更新(不清楚如何获得
不存在的
不应该存在的
的信息).@lamblichus谢谢你!我从另一个堆栈溢出线程中提取了此代码。我基本上有另一个API,它访问另一个站点,然后带回一个电子邮件列表。我正在获取其他API结果中的电子邮件列表-->将该列表与我的google成员组匹配-->删除任何不在原始API结果中的电子邮件-->添加任何不在原始API结果中的电子邮件。基本上,来自另一个网站的电子邮件列表应该是真相的来源,驱动着团队成员。在您的回复后,我更新了上面的代码,现在我在scp上获得的权限不足。请注意,我已确保我的令牌身份验证文件中有所有适当的作用域,以及直接在GCP中添加的适当作用域。我没有15+代表,否则我将投票支持此解决方案。这正是我想要的!!!我真的很感激API文档——我能够找到一些我在过程中遗漏的东西,比如实体ID等等。我真的很感激这花费的时间@不客气。您不需要15+代表就可以接受解决方案。看见
def updateGroupMembers(service):
    ideal_member_emails = ["member_1@example.com", "member_2@example.com", "member_3@example.com"]
    response_group = service.groups().list(customer='my_customer').execute()
    for group in response_group['groups']:
        group_email = group['email']
        response_members = service.members().list(groupKey=group_email).execute()
        if "members" in response_members:
            current_member_emails = list(map((lambda member : member["email"]), response_members["members"]))
            for ideal_member_email in ideal_member_emails:
                if ideal_member_email not in current_member_emails:
                    payload = {
                        "email": ideal_member_email
                    }
                    service.members().insert(groupKey=group_email, body=payload).execute()
            for current_member_email in current_member_emails:
                if current_member_email not in ideal_member_emails:
                    service.members().delete(groupKey=group_email, memberKey=current_member_email).execute()
        else:
            for ideal_member_email in ideal_member_emails:
                payload = {
                    "email": ideal_member_email
                }
                service.members().insert(groupKey=group_email, body=payload).execute()