Greetings,
I am using the ESRI sample code to generate a token (Scripting with the ArcGIS REST API—Portal for ArcGIS | Documentation for ArcGIS Enterprise ), getting an expected type error??
Error is Expected type 'Optional[bytes]', got 'str' instead??
I updated the code for Python 3.
### Python 3.8
def generateToken(username, password, portalUrl😞
'''Retrieves a token to be used with API requests.'''
portalUrl = myurl
username = myuser
password = mypassword
parameters = urllib.parse.urlencode({'username' : username,
'password' : password,
'client' : 'referer',
'referer' : portalUrl,
'expiration' : 60,
'f' : 'json'})
print("Read parameters")
response = urllib.request.urlopen(portalUrl + '/sharing/rest/generateToken?', parameters).read()
print("response")
try:
jsonResponse = json.loads(response)
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.')
## End
All I get is the error message:
Expected type 'Optional[bytes]', got 'str' instead
Appreciate any assistance and pointers.
Regards,
Clive
Solved! Go to Solution.
Hi Clive Swan,
You need to add encode(“utf-8”) at the end of line 14.
The code will be like so:
import urllib
import json
def generateToken(username, password, portalUrl):
'''Retrieves a token to be used with API requests.
portalUrl = myurl
username = myuser
password = mypassword'''
parameters = urllib.parse.urlencode({'username' : username,
'password' : password,
'client' : 'referer',
'referer' : portalUrl,
'expiration' : 60,
'f' : 'json'}).encode("utf-8")
print("Read parameters")
response = urllib.request.urlopen(portalUrl + '/sharing/rest/generateToken?', parameters).read()
print("response")
try:
jsonResponse = json.loads(response)
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.')
------------------------------------------------------------------------------------------------------------------------------------
Please mark as helpful if you find it helpful. If it answered your question please mark it as answered.
Hi Clive Swan,
You need to add encode(“utf-8”) at the end of line 14.
The code will be like so:
import urllib
import json
def generateToken(username, password, portalUrl):
'''Retrieves a token to be used with API requests.
portalUrl = myurl
username = myuser
password = mypassword'''
parameters = urllib.parse.urlencode({'username' : username,
'password' : password,
'client' : 'referer',
'referer' : portalUrl,
'expiration' : 60,
'f' : 'json'}).encode("utf-8")
print("Read parameters")
response = urllib.request.urlopen(portalUrl + '/sharing/rest/generateToken?', parameters).read()
print("response")
try:
jsonResponse = json.loads(response)
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.')
------------------------------------------------------------------------------------------------------------------------------------
Please mark as helpful if you find it helpful. If it answered your question please mark it as answered.
Thanks for the input on the .encode(utf-8)
I ran the code several times, nothing happened.. NOT even errors.
Then I remembered to add: generateToken(username, password, portalUrl ) at the end of the code.