Skip To Content




  • 用于访问 Portal for ArcGIS 的 URL。该 URL 可以是 ArcGIS Web Adaptor URL(例如,,也可以是托管门户的计算机 URL(例如,。指定 URL 时必须将计算机的完全限定域名包括在内。
  • 具有门户管理权限的帐户的用户名和密码。
  • 用于查询群组名称和确定将要添加的群组成员的关键字,例如 operations
  • 以逗号分隔的,将添加到通过关键字查询指定的群组的成员用户名的列表。

以下示例会将四个成员添加到与关键字 operations 相匹配的群组中:

python admin pass.word operations sarah,robert,james,qing
#!/usr/bin/env python
# Requires Python 2.7+
# Sample Usage:
# python <portal> <username> <password>
#                            <groupSearchString> <users>
# <users> should be entered as a comma separated string
import urllib
import json
import argparse
def generateToken(username, password, portalUrl):
    '''Retrieves a token to be used with API requests.'''
    parameters = urllib.urlencode({'username' : username,
                                   'password' : password,
                                   'client' : 'referer',
                                   'referer': portalUrl,
                                   'expiration': 60,
                                   'f' : 'json'})
    response = urllib.urlopen(portalUrl + '/sharing/rest/generateToken?',
        jsonResponse = json.loads(response)
        if 'token' in jsonResponse:
            return jsonResponse['token']
        elif 'error' in jsonResponse:
            print jsonResponse['error']['message']
            for detail in jsonResponse['error']['details']:
                print detail
    except ValueError, e:
        print 'An unspecified error occurred.'
        print e
def groupSearch(query, token, portalUrl):
    '''Search for groups matching the specified query.'''
    # Example 1: query all groups owned by a user.
    # 'owner:johndoe'
    # Example 2: query groups with Operations in the name.
    # 'Operations'
    # Example 3: query all groups with public access.
    # 'access:public'
    parameters = urllib.urlencode({'q': query, 'token': token, 'f': 'json'})
    request = (portalUrl + '/sharing/rest/community/groups?' + parameters)
    groups = json.loads(urllib.urlopen(request).read())
    return groups['results']
def addUsersToGroups(users, groups, token, portalUrl):
    Add users to multiple groups and return a list of the status.
    # Provide one or more usernames in a list.
    # e.g. ['john_doe', 'jane_doe']
    # Provide one or more group IDs in a list.
    # e.g. ['d93aabd856f8459a8905a5bd434d4d4a',
    #       'f84c841a3dfc4591b1ff83281ea5025f']
    toolSummary = []
    # Assign users to the specified group(s).
    parameters = urllib.urlencode({'token': token, 'f': 'json'})
    for group in groups:
        response = urllib.urlopen(portalUrl +
                                   '/sharing/rest/community/groups/' +
                                   group + '/addUsers?',
                                   'users=' + ','.join(users) + "&" +
        # The response will only report back users that
        # were NOT successfully added.
        toolSummary.append({'id': group,
                            'results': json.loads(response)})
    return toolSummary
# Run the script.
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
                        help=('url of the Portal (e.g. '
    parser.add_argument('username', help='username')
    parser.add_argument('password', help='password')
    parser.add_argument("query", help="group search string")
    parser.add_argument("users", help="a comma-separated string of users")
    # Read the command line arguments.
    args = parser.parse_args()
    portal = args.portal
    username = args.username
    password = args.password
    query = args.query
    users = (args.users).split(',') # Create a list from the input users.
    token = generateToken(username=username, password=password,
    # Get a list of the groups matching the query.
    groupIDs = []
    groups = groupSearch(query, token, portal)
    for group in groups:
    results = addUsersToGroups(users=users, groups=groupIDs, token=token,
    for group in results:
        if len(group['results']['notAdded']) > 0:
            # Get the group name.
            groupName = groupSearch('id:' + group['id'], token,
            print 'The following users were not added to ' + groupName
            for user in group['results']['notAdded']:
                print '    ' + user
    print 'Process complete.'