2015-01-27 46 views
0

我做下面創建一個用戶資源做SDK管理目錄API一個user.insert在Python腳本:如何創建Python腳本一個user.alias資源

userinfo = { 'primaryEmail': '[email protected]', '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': '[email protected]'} 

service.users().alias.insert([email protected], body=userinfo).execute 

獲取:

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

我解決如下:service.users()。別名()。insert(userKe[email protected],body = userinfo).execute – Tim 2015-01-29 00:17:01

回答

1

我解決如下:。

service.users()別名()插入([email protected],身體=用戶信息).execute

我曾嘗試用戶()。別名()...但需要別名()。

0

下面是我用它來刪除所有谷歌電子郵件別名,然後從一個形式(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 = ['[email protected]', '[email protected]'] 
     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 
相關問題