Remove Portal user via ArcGIS REST API with Python without arcgis module

601
1
06-20-2018 08:11 AM
TobiasFimpel
Occasional Contributor

I want to use Python without needing to load the arcgis module (mainly to keep licensing simpler) to remove a Portal user account. It seems like a call to http:// <portal-url>/removeusers as described here should be easy enough to make. But I can't get it to actually work. Can somebody provide an example?

Here is the function I have, based on a number of somewhat similar examples I found online.

def removeUser (token,portalUrl,username)
    params = urllib.urlencode({'token' : token,'f' : 'json','users' : username})
    response = json.loads(urllib.Request(portalUrl + '/removeusers?', params, { 'Referer' : portalUrl }).read())

The error I get is "AttributeError: read" .

0 Kudos
1 Reply
JonathanQuinn
Esri Notable Contributor

Request only builds a request that you still have to open using urllib2.urlopen, (I also assume you had a copy/paste issue as Request is a function of urllib2, not urllib).

def removeUser (token,portalUrl,username):
    params = urllib.urlencode({'token' : token,'f' : 'json','users' : username})
    req = urllib2.Request(portalUrl + '/removeusers?', params, { 'Referer' : portalUrl })
    response = json.loads(urllib2.urlopen(req).read())
    print(response)

20.6. urllib2 — extensible library for opening URLs — Python 2.7.15 documentation