Select to view content in your preferred language

quest for the token (logging in to our agol using python)

3688
8
04-12-2016 04:51 AM
BertKraan1
Frequent Contributor

We use arcmap 10.3 and arcgis online. I want  to be able to download/backup  or upgrade hosted feature layers using python in order to automate our processes.

The last couple of days have been an education ... unfortunately thus far I didn't graduate

I understand now that I have to authenticate in order to get a token and use that token to get access to the feature layer, can anybody confirm this is correct? There seem to be several ways to do this?

First, the good news: What Works At The Moment:

     I cán use a client_id/client_secret I got after registering at developers.arcgis.com to access a hosted feature layer that is shared with everybody: (code from developers.arcgis.com, sorry didn't find a code tag)

code_demo_developers.PNG

the response is first the token and second a lot of data:

Capture.PNG

When I  try the same with a hosted feature layer that is only shared with a group local to my agol I get a neat response as well:

Capture.PNG

As expected this id/secret does not have permissions to that feature layer. So far, so good!

Now, my problem is how to use one of my 'local' arcgis online users to get access. For instance I have a user b.jansen_kdhl which only exists in kdhl.maps.arcgis.com. I want this user to be able to start a python script which updates a feature layer I've hosted on my arcgis online. The user as such has appropriate permissions.

How can I achieve to get a valid token for such a local agol user? Is there a specific tokenserver to use? Is the tokenserver/url a part of my own agol? Or am I barking up the wrong tree here?

Thanks for reading, thanks in advance for your replies.

regards,

Bert

Tags (3)
0 Kudos
8 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Bert,

Take a look at the following tool for downloading a hosted feature service:

Download ArcGIS Online Feature Service or ArcGIS Server Feature/Map Service

Within the tool includes code to generate a token.  Ex:

tokenURL = 'https://www.arcgis.com/sharing/rest/generateToken'
params = {'f': 'pjson', 'username': username, 'password': password, 'referer': 'http://www.arcgis.com'}
req = urllib2.Request(tokenURL, urllib.urlencode(params))
response = urllib2.urlopen(req)
data = json.load(response)
token = data['token']
BertKraan1
Frequent Contributor

Hi Jake,

I've got that tool but I don't get it. I've already posted some additional questions in that thread.

We are at the end of our working day here in the Netherlands but I'll have a fresh attempt tomorrow morning 🙂

Bert

0 Kudos
BertKraan1
Frequent Contributor

When I use your toolbox in arcmap I can download a feature layer. When I try the script from a python IDE I can't.

now the stack data reveils that url, username and password are set as expected, and I can see I do have a token but the error message is: URL is incorrect. Or, Service is secure, please enter username and password.

I'm using the same URL, username and password as I'm using in the toolbox. I do not understand why the toolbox works but the python script does not.

Is the token valid for the url I'm trying to reach? Or should the token be generated elsewhere?

0 Kudos
JakeSkinner
Esri Esteemed Contributor

The script has 10 total parameters, 7 of which are required.  If you do not set the required parameters, the script will fail.  The GP tool sets most of the parameters automatically using default values.  If you right-click on the GP tool in ArcMap > Properties > Parameters tab, you can scroll through each parameter to see which are required.

BertKraan1
Frequent Contributor

Jake, thanks for that, i've set the parameters.

I just succeeded in using my own username/password to obtain some info from a feature service on my own agol, that's defenitely a step. Now I'm playing around with the result. Still a lot of python to be learned...

0 Kudos
by Anonymous User
Not applicable

Did you ever get a solution to this?  I'm running into the same issue when trying to import his toolbox and run it via a script.

0 Kudos
by Anonymous User
Not applicable

I realized that when the password string is passed in the loop, after each iteration it is replaced with "******".  It seems to work in a loop if the password string is entered as a variable.

0 Kudos
BertKraan1
Frequent Contributor

Hi Graham,

Sorry I only now see your post, I'm not a lot on geonet nowadays.

Yes, we've got it working. Did you succeed?

0 Kudos