Looking for an example Python https-post script which communicates with portal-api

2264
5
Jump to solution
08-20-2021 02:05 AM
KlausWieland
New Contributor II

I am using a Python script to change roles of users, etc also creating roles is working.

but wenn i go to set-privileges of the role i get an error


{u'error': {u'messageCode': u'ORG_1064', u'message': u"Invalid JSON in 'privileges' parameter.", u'code': 400, u'details': []}}

 

maybe it has to do with, that i do an URLENCODE of the post-data before sending to the API.

This worked for the other requests, but not for the set-privileges request, whre i have to send a list...

further i try to send the json directly to the api or via JSON.DUMPS, but i did not get it working yet.

do you have an working example how i can send post data to set privileges in python?

on the machine i have only arcgis portal and arcgis server. i dont have libraries of other arcgis-software

 

 

0 Kudos
1 Solution

Accepted Solutions
KlausWieland
New Contributor II

Now it works ... Let me say, that is not intuitive!

 

So what I send to the api, what is working is:

{ … 'privileges': {'privileges': [ … ] } … }

 

I am sure this is not a bug, it is a feature 😉

 

View solution in original post

0 Kudos
5 Replies
KlausWieland
New Contributor II

this is not working:

r = requests.post(url, data=data,headers=headers,verify=False)

r = requests.post(url, data=json.dumps(data),headers=headers,verify=False)

r = requests.post(url, json=data,headers=headers,verify=False)

everytime i get an error 403

 

0 Kudos
GaetanPRU
New Contributor III

You can try this method, example to generate token :

 

import http, urllib.parse, json, ssl

gcontext = ssl.SSLContext()

URL = "https://xxxxxxxxxx/arcgis/admin/generateToken"

postdata = {'username': "xxxxxxxxxx", 'password': "xxxxxxxxxx", 'client': 'requestip', 'f': 'json'}
postdata = urllib.parse.urlencode(postdata).encode("utf-8")
response = urllib.request.urlopen(URL, data=postdata, context=gcontext)

data = response.read()
response.close()
tokenAGS = json.loads(data)

 

You will just have to check that you are using the correct parameters in postdata. You can use this doc to see what parameters is used in request : https://developers.arcgis.com/rest/ 

Your error seems to indicate a problem with the JSON.

Maybe the easyest way to do this it's to use ArcGIS API for Python to manage user : https://developers.arcgis.com/python/guide/accessing-and-managing-users/

GaetanPRU
0 Kudos
MichaelNüßlein
New Contributor III

This is a question - NOT a solution.

 

This is an python3 example, right?

It is not working here: response = urllib.request.urlopen(URL, data=postdata, context=gcontext)
AttributeError: 'module' object has no attribute 'request'

 

0 Kudos
GaetanPRU
New Contributor III

Yes it's a python 3 example.

I'm not sure of the reason but it depends of the import of the module. Try to add module "urllib.request" in import.

Maybe my example has this import elsewhere and I don't copy it here. I try it with Jupyter Notebook with ArcGIS PRO.

GaetanPRU
KlausWieland
New Contributor II

Now it works ... Let me say, that is not intuitive!

 

So what I send to the api, what is working is:

{ … 'privileges': {'privileges': [ … ] } … }

 

I am sure this is not a bug, it is a feature 😉

 

0 Kudos