Trouble Generating Token Using Portal Rest API

857
5
Jump to solution
06-02-2022 11:56 AM
Labels (1)
JasonBartling1
New Contributor II

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.

0 Kudos
1 Solution

Accepted Solutions
JasonBartling1
New Contributor II

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.

View solution in original post

0 Kudos
5 Replies
ChristopherPawlyszyn
Esri Contributor

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.


-- Chris Pawlyszyn
0 Kudos
JasonBartling1
New Contributor II

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)
0 Kudos
ChristopherPawlyszyn
Esri Contributor

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)

 

 


-- Chris Pawlyszyn
0 Kudos
JasonBartling1
New Contributor II

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.

0 Kudos
JasonBartling1
New Contributor II

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.

0 Kudos