尝试通过 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 群组时出现“缺少必填字段:成员” 的相关文章

随机推荐