Using Python to generate access token for an account that uses organization login

6023
11
01-13-2022 01:22 PM
FelipeDias
New Contributor II

Hi there!

I've been trying to generate a REST API access token for an item on ArcGIS online through a basic Python script and I'm having a hard time.

Here are the URLs of the item that I want to access:

Note: You won't be able to access the item above because it's not shared publicly (which is why I want to generate an access token in the first place). 

I followed the instructions found here (Generate Token in REST) and arrived at the following Python code (substituting the placeholders with my actual username and password):

 

 

import requests
tokenURL = 'https://services9.arcgis.com/sharing/rest/generateToken'
params = {'f': 'pjson', 
          'username': 'placeholder_username', 
          'password': 'placeholder_password', 
          'referer': 'http://www.arcgis.com'}
response = requests.post(tokenURL, data = params, verify = False)
token = response.json()['token']

 

 

 

 

The problem is that the code above doesn't work. When I investigate the response object, I notice two things: 

  • It doesn't have a "token" key.
  • It tells me that the URL I used is invalid.

 

 

 

print(response.json()) 
# {'error': {'code': 400, 'message': 'Invalid URL', 'details': ['Invalid URL']}}

 

 

 

So I tried using tokenURL='https://www.arcgis.com/sharing/rest/generateToken', but that didn't work either: 

 

 

 

print(response.json()) 
# {'error': {'code': 400, 'message': 'Unable to generate token.', 'details': ['Invalid username or password.']}}

 

 

 

 I believe this might be because I typically log into ArcGIS online through my organization's URL: mycompany.maps.arcgis.com (as seen below):

 

FelipeDias_0-1677171213329.png

 

So I finally tried to use tokenURL='https://mycompany.maps.arcgis.com/sharing/rest/generateToken', but that didn't work either:

 

 

 

print(response.json()) 
# {'error': {'code': 400, # 'message': 'Unable to generate token.', # 'details': ['Invalid username or password.']}}

 

 

 

Does anyone know what I need to do to successfully generate an access token for the item above? 

0 Kudos
11 Replies
LongDinh
Occasional Contributor II

Hi @FelipeDias ,

Try using this endpoint instead /sharing/generateToken . Your request should be something like:

 

 

import requests
organisationName = "orgName"
url = f"https://{organisationName}.maps.arcgis.com/sharing/generateToken"

username = "NamedUserAccount"
password = "Password123"

payload={'f': 'json',
'username': username,
'password': password,
'referer': f"https://{organisationName}.maps.arcgis.com/"}

response = requests.request("POST", url, data=payload)

print(response.json())

 

 

You can check to see if the token works by requesting some content from your named user's contents with the following request:

 

 

organisationName = "orgName"

url = f"https://{organisationName}.maps.arcgis.com/sharing/rest/content/users/{username}"

token = response.json()['token']
params={
    'f':'json',
    'num':1,
    'token': token
}

userContents = requests.request("GET", url, params=params)

print(userContents.json())

 

 

 

0 Kudos
FelipeDias
New Contributor II

Thanks for the quick reply!!! Sadly, it didn't work, though... This is what got inside the `response` item:

print(response.json())
{'error': {'code': 400, 
 'message': 'Unable to generate token.', 
 'details': ['Invalid username or password.']}}

 Any idea what's going on?

0 Kudos
LongDinh
Occasional Contributor II

Are you using a valid account? It needs to be a named user account 

 

0 Kudos
FelipeDias
New Contributor II

I am. Here's a snapshot of my user profile (I've edited the actual username for privacy):

FelipeDias_0-1677171344188.png

 

But as I mentioned in my original post, I don't log in to ArcGIS online though the default portal. Instead, I use my company's organization URL. I have the feeling that that is what's causing the issues here.

0 Kudos
LongDinh
Occasional Contributor II

That looks like a Azure Directory account linked to your organisation's ArcGIS Online. You should either:

  1. Use your organisation's ArcGIS Online named user administration account, or
  2. Have your ArcGIS Online named user administration account create a named user for purposes of generating tokens.

The later is generally the preferred solution as you should set up the contents/groups shared to the named user so that it has limited access to items in your organisation's ArcGIS Online

0 Kudos
FelipeDias
New Contributor II

Great, I'll reach out to our IT folks and see if they can generate that for me. I'll report back here if it worked later! Thanks!!!!

alex_friant
Occasional Contributor II

Did it end up working?

0 Kudos
MarkGambordella
New Contributor III

Also would like to know if it worked.  I work for the City and sign in through our organizations login.  I have a script that i run but get the following error message:  

Error: Line 34 -- "token = response.json()['token']": 'token'
Failed script Attachments to Popup...
Failed to execute (AttachmentstoPopup).

0 Kudos
MarkGambordella
New Contributor III

I found a work around.  Had to create a built-in account and use that as the user/password instead of the SAML account.  It worked and was able to generate a token using this geoprocessing tool below.

 Show Attachments in Web Map Popup - Page 9 - Esri Community