I am attempting to generate a token from REST using Python for portal admin but only receiving the generateToken page in HTML as a response. We are running Enterprise version 10.8.1. This is my code:
# A function to generate a token given username, password and the adminURL.
def getToken(username, password, baseURL):
# Token URL is typically http://server[:port]/arcgis/admin/generateToken
tokenURL = "{}arcgis/sharing/rest/generateToken".format(baseURL)
params = urllib.parse.urlencode({'username' : username,
'password' : password,
'client' : 'referer',
'referer': baseURL,
'expiration': 60,
'f' : 'json'})
headers = {"Content-Type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
# Read response
response = requests.post(tokenURL, data=params, headers=headers)
return response.text
I have used similar syntax before to generate a token with ArcGIS Server admin version 10.8.1, and that code still works. Any reason why the Portal generateToken would be more finicky, or what the problem might be here? It seems like something with the request must be off to not return the token in response.
Solved! Go to Solution.
It turns out the solution for me was to use the portal web adapter URL instead of the FQDN with the port number. This is the update to the baseURL parameter I had to make:
baseURL = "https://webserverDNSentry/webAdapterName/"
Thanks to Vysakh, the ESRI support services analyst I was working with, that helped me figure this out.
Can you try without encoding the parameters? Since the values are sent in the body of the request there shouldn't be a need to encode the referer URL.
That's the only difference I can see between my working version and yours. I also leave off the headers for the token request but I don't expect that to make a difference.
I get the same HTML page response as before using:
tokenURL = "{}arcgis/sharing/rest/generateToken".format(baseURL)
params = {'username' : username,
'password' : password,
'client' : 'requestip',
'referer': baseURL,
'expiration': 60,
'f' : 'json'}
# Read response
response = requests.post(tokenURL, data=params)
Using this code I successfully generated a token on 10.8.1, 10.9, and 10.9.1 Portal sites. The big difference I see is that you're using 'requestip' for the client as opposed to 'referer'.
import requests
baseURL = 'https://<FQDN>:7443/arcgis'
tokenURL = "{}/sharing/rest/generateToken".format(baseURL)
username = <IAA_account>
password = <IAA_password>
expiration = 60
params = {'username': username, 'password': password, 'client': 'referer', 'referer': baseURL, 'expiration': expiration, 'f': 'json'}
response = requests.post(tokenURL, data=params, verify=False)
token = response.json()['token']
print(token)
Sorry about leaving the requesip in there. That was from a previous attempt. I copy and pasted your code into a jupyter notebook I have going, and I am still not getting a token back.
It turns out the solution for me was to use the portal web adapter URL instead of the FQDN with the port number. This is the update to the baseURL parameter I had to make:
baseURL = "https://webserverDNSentry/webAdapterName/"
Thanks to Vysakh, the ESRI support services analyst I was working with, that helped me figure this out.