如何为python脚本创建user.alias资源

时间:2015-01-27 00:12:14

标签: python insert alias

我执行以下操作来创建用户资源,以便在python脚本中为SDK Admin目录API执行user.insert:

userinfo = { 'primaryEmail': 'joe@xxx.yyy', 'name': { 'given_name': 'joe', 'familyName': 'smith' }, 'password': 'password' }

service = build("admin", "directory_v1", http=http)

service.users().insert(body=userinfo).execute()

How do I similarly create a user.alias resource so that I can do something like:

userinfo = {'alias': 'js@xxx.yyy'}

service.users().alias.insert(userKey=joe@xxx.yyy, body=userinfo).execute

获得:

AttributeError: 'Resource' object has no attribute 'alias' ... or variants thereof with modifications. Twisted in the syntax for "user.alias" resources.

2 个答案:

答案 0 :(得分:1)

我解决如下:

service.users()。aliases()。insert(userKey=joe@xxx.yyy,body = userinfo).execute

我曾尝试过users()。alias()...但需要别名()。

答案 1 :(得分:0)

这是我用来删除所有Google电子邮件别名的函数,然后从表单中提交的地址(Django)创建一组新的:

def google_set_aliases(request, username, aliases):
    '''
    Resets the list of email aliases for a user in the Google directory.
    To ensure that LDAP is canonical, we first delete all Google aliases,
    then insert the new list. Takes `aliases` as a python list of email addresses.

    e.g. (in another function):
        aliases = ['foo@exmaple.com', 'bar@example.com']
        google_set_aliases(request, 'msmith', aliases)

    '''
    try:
        # google_get_auth() is a separate func that gets an authenticated handle on a
        # with a passed-in scope (using 2LO).
        http_auth = google_get_auth(scope='https://www.googleapis.com/auth/admin.directory.user.alias')
        service = build("admin", "directory_v1", http=http_auth)

        # Get the user's complete set of Google-stored aliases.
        google_aliases = service.users().aliases().list(userKey="{u}@example.com".format(u=username)).execute()

        # If returned set is non-empty, delete them all.
        if 'aliases' in google_aliases:
            templist = [entry['alias'] for entry in google_aliases['aliases']]
            for alias in templist:
                service.users().aliases().delete(userKey="{u}@example.com".format(u=username), alias=alias).execute()

        # Now insert new aliases. Google automatically drops existing users so we don't have to check for conflicts.
        for alias in aliases:
            service.users().aliases().insert(userKey="{u}@example.com".format(u=username), body={'alias': alias}).execute()

        return True
    except:
        return False