尝试通过 API 将成员添加到 Google 群组时出现“缺少必填字段:成员”

2024-05-05

尝试使用 Google 管理目录 API 来读取 google 群组(组织)的成员 - 它工作正常。当我尝试添加成员时,我得到:

{ errors: 
   [ { domain: 'global',
       reason: 'required',
       message: 'Missing required field: member' } ],
  code: 400,
  message: 'Missing required field: member' }

我用谷歌搜索了错误并发现了类似的问题this https://stackoverflow.com/questions/22765599/missing-required-field-member, this https://stackoverflow.com/questions/27057265/google-groups-directory-api-add-user-to-group-raises-error-php以及其他一些无用的结果。

我查了一下,确实是not缺少范围或权限。

#!/usr/bin/python
import httplib2
import json
from oauth2client.client import SignedJwtAssertionCredentials
from urllib import urlencode


def get_group_members(group):
    url = 'https://www.googleapis.com/admin/directory/v1/groups/{}/members'.format(group['email'])
    return call_google_api("GET", url)


def add_group_member(group, payload=False):
    url = 'https://www.googleapis.com/admin/directory/v1/groups/{}/members'.format(group)
    return call_google_api("POST", url, payload)


def call_google_api(method, url, payload=False):
    content = {}
    try:
        http = get_conn()
        if payload:
            (resp, content) = http.request(uri=url, method=method, body=urlencode(payload))
        else:
            (resp, content) = http.request(uri=url, method=method)
    except Exception as e:
        print "Failed to post request to [{}] due to: {}".format(url, e)
    return json.loads(content)

def get_conn():
    client_email = get_client_email_from_db()    
    with open(get_private_key_filename()) as f:
        private_key = f.read()

    oauth_scope = ['https://www.googleapis.com/auth/admin.directory.group.member', 
                  'https://www.googleapis.com/auth/admin.directory.group',
    ]

    credentials = SignedJwtAssertionCredentials(client_email, private_key, oauth_scope, sub='[email protected] /cdn-cgi/l/email-protection')
    http = httplib2.Http()
    return credentials.authorize(http)


if __name__ == '__main__':
    payload = {
        "email": "[email protected] /cdn-cgi/l/email-protection",
        "role": "MEMBER",
    }
    print "\n ---------------------------------- \n"
    print "calling add_group_member('[email protected] /cdn-cgi/l/email-protection', '[email protected] /cdn-cgi/l/email-protection')"
    res = add_group_member("[email protected] /cdn-cgi/l/email-protection", payload)
    print "\n ---------------------------------- \n"

Comment:
我通过使用sdk成功实现了我想要的apiclient.discovery.build,但仍然 - 我很好奇,问题是什么以及是否可以解决。

调试请求:

connect: (www.googleapis.com, 443)
send: 'POST /admin/directory/v1/groups/[email protected] /cdn-cgi/l/email-protection/members HTTP/1.1\r\nHost: www.googleapis.com\r\nContent-Length: 38\r\ncontent-type: application/json\r\naccept-encoding: gzip, deflate\r\nauthorization: Bearer ya29.RAFzf3hyxvP0LuR4VdpqKr_dD0WzOcvXjn4eWV5Em6xJDissi4ieOZ2ZBRMOP-WLhvTrecBxgF_6sznc1GKSWHanvgYTh_EzcilsAN0f5jOiiMahOadG2v5ixBPL9GcqebRdz_kQc1y2iQ\r\nuser-agent: Python-httplib2/0.9 (gzip)\r\n\r\nrole=MEMBER&email=alfasi%40xxxx.com'
reply: 'HTTP/1.1 400 Bad Request\r\n'
header: Vary: Origin
header: Vary: X-Origin
header: Content-Type: application/json; charset=UTF-8
header: Content-Encoding: gzip
header: Date: Sat, 28 Mar 2015 23:14:47 GMT
header: Expires: Sat, 28 Mar 2015 23:14:47 GMT
header: Cache-Control: private, max-age=0
header: X-Content-Type-Options: nosniff
header: X-Frame-Options: SAMEORIGIN
header: X-XSS-Protection: 1; mode=block
header: Server: GSE
header: Alternate-Protocol: 443:quic,p=0.5
header: Transfer-Encoding: chunked

由于 google API 使用(仅?)JSON 编码,因此您的帖子数据不会被解析为所需的成员对象。您已经加载了响应的 json,因此您只需要更改编码,并可以选择显式指示它:

 if payload:
        (resp, content) = http.request(uri=url, method=method, body=urlencode(payload))
 # becomes:   
 if payload:
        (resp, content) = http.request(uri=url, method=method, body=json.dumps(payload), headers={'Content-type':'application/json'})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尝试通过 API 将成员添加到 Google 群组时出现“缺少必填字段:成员” 的相关文章

  • 类型错误:object.__new__(int) 不安全,请使用 int.__new__()

    阅读本文时 Python 中的元类是什么 https stackoverflow com questions 100003 what is a metaclass in python 我正在学习使用 new 使用以下代码片段 class a
  • 使用“iloc”时出现“尝试在 DataFrame 切片的副本上设置值”错误

    Jupyter 笔记本返回此警告 C anaconda lib site packages pandas core indexing py 337 SettingWithCopyWarning A value is trying to be
  • 在 Python 中搜索文本文件并打印相关行?

    如何在文本文件中搜索关键短语或关键字 然后打印关键短语或关键字所在的行 searchfile open file txt r for line in searchfile if searchphrase in line print line
  • 如何分组显示argparse子命令?

    对于具有许多子命令的程序 我想在 help 输出中显示它们按逻辑分组 Python argparse 有一个add argument group http docs python org library argparse html argp
  • 合并一个对(元组)列表?

    从链接对的列表中 我想将这些对组合成公共 ID 组 这样我就可以将 group ids 写回数据库 例如 UPDATE table SET group n WHERE id IN Example 1 2 3 4 1 5 6 3 7 8 be
  • 在 Flask 中将配置文件作为字典读取

    在 instance app cfg 我已经配置 test test 在我的烧瓶文件 app py 中 with app open instance resource app cfg as f config f read print con
  • 字典键中的通配符

    假设我有一本字典 rank dict V 1 A 2 V 3 A 4 正如您所看到的 我在一个 V 的末尾添加了一个 虽然 3 可能只是 V 的值 但我想要 V1 V2 V2234432 等的另一个密钥 我想检查它 checker V30
  • 带剖面的 3D 曲面图

    基本上 我有一个由一组时间序列组成的曲面图 我想在特定高度添加剖面图 以更好地了解一年中值高于所选阈值的时期 由此 其中显示平面但不是剖面 To This 有什么建议吗 使用 alpha 和相机仰角并没有解决问题 平面似乎仍然在人物的前面
  • XGBOOST 功能名称错误 - Python

    也许这个问题已经以不同的形式被问过很多次了 但是 我的问题是当我使用XGBClassifier 对于像数据这样的产品 我收到功能名称不匹配错误 我希望有人能告诉我我做错了什么 这是我的代码 顺便说一句 数据完全是编造的 import pan
  • 使用 Python 将 Json 转换为换行 Json 标准

    我有一个获取嵌套对象并删除所有嵌套的代码 使对象平坦 def flatten json y param y Unflated Json return Flated Json out def flatten x name if type x
  • 如何在Python中使用内联正则表达式修饰符[重复]

    这个问题在这里已经有答案了 我有一个正则表达式 n DOCUMENTATION n n n 2 s 女巫我正在尝试处理这样的一些文件 usr bin python coding utf 8
  • While 在范围内循环用户输入

    我有一些代码 我想要求用户输入 1 100 之间的数字 如果他们在这些数字之间输入一个数字 它将打印 Size input 并打破循环 但是 如果他们在外部输入一个数字1 100 它将打印 大小 输入 并继续向他们重新询问一个数字 但我遇到
  • Python 结构的 PHP 替代品

    我很高兴在我的 Python 项目中使用 Fabric 进行部署 现在我正在从事一个更大的 PHP 项目 想知道是否有类似 PHP 的 Fabric 之类的东西 唔 为什么这有关系 Fabric 只是 python 脚本 所以它与项目语言无
  • 如何从张量流数据集迭代器返回同一批次两次?

    我正在转换一些旧代码以使用数据集 API 此代码使用feed dict将一批数据送入列车运行 实际上是三次 然后重新计算损失以供显示使用同一批 所以我需要一个迭代器来返回完全相同的批次两次 或多次 不幸的是 我似乎找不到一种使用张量流数据集
  • .NET 是否有相当于 Python 中的 **kwargs 的功能?

    我一直无法通过典型渠道找到这个问题的答案 在Python中我可以有以下函数定义 def do the needful kwargs Kwargs is now a dictionary i e do the needful spam 42
  • 如何在 django-rest-framework 查询集响应中添加注释数据?

    我正在为查询集中的每个项目生成聚合 def get queryset self from django db models import Count queryset Book objects annotate Count authors
  • 如何在 Windows 10 上将 ipynb 文件与 Jupyter Lab(来自 Anaconda)关联

    我使用 Windows 10 Jupiter Lab 是从 Anaconda 安装的 我想交往ipynb使用 Jupyter Lab 保存文件 这样 当我双击ipynb文件应使用 Jupyter Lab 打开 我该怎么做 Install n
  • 通过 Selenium 和 python 切换到 iframe

    我如何在硒中切换到这个 iframe 只知道 您可以使用 XPath 来定位 iframe driver find element by xpath iframe name Dialogue Window Then switch to th
  • 删除aws beanstalk上的uuid python包

    这是针对所提出问题的后续帖子 问题here https stackoverflow com questions 44421761 flask beanstalk deployment errors 以防万一对其他人有用 自从第一篇文章以来
  • Python 3d 金字塔

    我是 3D 绘图新手 我只想用 5 个点建造一个金字塔并通过它切出一个平面 我的问题是我不知道如何填充两侧 points np array 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 fig plt figure ax fi

随机推荐