Greetings
I am working on an ESRI Hosted site.
I am trying to automate the ESRI token web page Generate Token form ie:
I used a Python POST:
user_name = 'me'
pass_word = 'my password'
referer = 'https://company.cloud.esriuk.com'
referer_name = 'https://company.cloud.esriuk.com/portal/sharing/rest/generateToken'
token = ''
expires = 90
##data = {username=user_name,password=pass_word, client=referer, referer=url, expiration=90, f=json}
requests.post(user=user_name, password=pass_word, client=referer, referer=referer_name, expiration=90, f=json)
print("Generating token")
I also tried:
gis_server = GIS(url=f"server_base_url/web_adaptor/admin",
token_url=f"server_base_url/web_adaptor/tokens/generateToken",
username="admin_user",
password="admin_password")
print(token)
I just get the process finished, no output??
Any suggestions?
Regards,
Clive
Solved! Go to Solution.
Greetings,
I found this link that solves the problem. Short simple and does what needs to be done.
import requests
portalUrl = "https://domain/portal"
username = 'username'
password = "password"
def generateToken(username, password, portalUrl):
# Retrieves a token to be used with API requests.
headers = {'content-type': 'application/x-www-form-urlencoded'}
parameters = {'username': username,
'password': password,
'client': 'referer',
'referer': portalUrl,
'expiration': 60,
'f': 'json'}
url = portalUrl + '/sharing/rest/generateToken?'
response = requests.post(url, data=parameters, headers=headers)
try:
jsonResponse = response.json()
if 'token' in jsonResponse:
return jsonResponse['token']
elif 'error' in jsonResponse:
print (jsonResponse['error']['message'])
for detail in jsonResponse['error']['details']:
print (detail)
except ValueError:
print('An unspecified error occurred.')
print(ValueError)
token = generateToken(username, password, portalUrl)
print(token)
Hi David,
Thanks for the response.
I am getting a HTTP:400 error now, stating that the client_id is not specified??
I have hardcoded the client/client_id/referer as company name and/or the base URL. But still keep getting the 'client_id 'not specified' error message??
Odd.
Thanks,
Clive
I'm not sure what you mean exactly, you shouldn't need to modify anything within the generate token function itself.
I'm guessing you have the esri arcgis GIS module in your library? You could:
from arcgis.gis import GIS
#params
portal_url = "https://domain/portal"
username = "me"
password = "P@ssword123"
#do stuff
connection = GIS(portal_url, username, password)
token = connection._con.token
print(token)
Since client/client_id/referer are not the same thing, can you share your code again?
Hi Joshua,
The client is a Webapp URL: https://company.cloud.esriuk.com/
The code is:
######
username = 'me'
password = 'my password
referer = "https://company.maps.arcgis.com"
portalUrl = "https://company.cloud.esriuk.com/portal"
tokenURL = "https://company.cloud.esriuk.com/portal/sharing/rest/generateToken"
print("START Generate Token")
def generateToken(user_name, pass_word, portalUrl):
# Retrieves a token to be used with API requests.
headers = {'content-type': 'application/x-www-form-urlencoded'}
parameters = {'username': username, 'password': password, 'client': 'referer', 'referer': portalUrl, 'expiration': 60, 'f': 'json'}
url = 'https://eiffagekier.maps.arcgis.com/sharing/rest/oauth2/token'
response = requests.post(url, data=parameters, headers=headers)
#print(response) ### >>>> ### GET error
try:
jsonResponse = response.json()
print(jsonResponse)
if 'token' in jsonResponse:
return jsonResponse['token']
elif 'error' in jsonResponse:
print (jsonResponse['error']['message'])
for detail in jsonResponse['error']['details']:
print (detail)
except ValueError:
print('An Error occurred.')
print(ValueError)
token = generateToken(username, password, tokenURL) #### portalUrl
print(token)
print("END Generate Token")
Thanks,
Clive
Greetings,
I found this link that solves the problem. Short simple and does what needs to be done.