Generate token from portal URL

1768
3
Jump to solution
12-11-2017 01:13 AM
MagalyClément-Zaber
Occasional Contributor

Hi,

Since I update arcgis 104.1 to 10.5.1, I have an issue when i want to generate token :

parameters = urllib.urlencode({'username' : username,
 'password' : password,
 'client' : 'referer',
 'referer': portalUrl,
 'expiration': 60,
 'f' : 'json'})
 response = urllib.urlopen(portalUrl + '/sharing/rest/generateToken?', parameters).read()
 try:
 jsonResponse = json.loads(response)
 if 'token' in jsonResponse:
 self.token = jsonResponse['token']
 return self.token
 elif 'error' in jsonResponse:
 print(jsonResponse['error']['message'])
 for detail in jsonResponse['error']['details']:
 print(detail)
 sys.exit(2)
 except ValueError, e:
 print('An unspecified error occurred.')
 print(e)

IOError: [Errno socket error] [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)

Do you have any idea to solve this issue ?

Thx.

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Hi Magaly,

Try the following.  The try/except should bypass the SSL Certificate error.

import urllib, urllib2, json, ssl

username = "portal"
password = "agol123"

tokenURL = 'https://portal.esri.com/portal/sharing/rest/generateToken/'
params = {'f': 'pjson', 'username': username, 'password': password, 'referer': 'https://portal.esri.com'}
req = urllib2.Request(tokenURL, urllib.urlencode(params))
try:
    response = urllib2.urlopen(req)
except:
    gcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
    response = urllib2.urlopen(req, context=gcontext)
data = json.load(response)
token = data['token']
print(token)

View solution in original post

3 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Magaly,

Try the following.  The try/except should bypass the SSL Certificate error.

import urllib, urllib2, json, ssl

username = "portal"
password = "agol123"

tokenURL = 'https://portal.esri.com/portal/sharing/rest/generateToken/'
params = {'f': 'pjson', 'username': username, 'password': password, 'referer': 'https://portal.esri.com'}
req = urllib2.Request(tokenURL, urllib.urlencode(params))
try:
    response = urllib2.urlopen(req)
except:
    gcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
    response = urllib2.urlopen(req, context=gcontext)
data = json.load(response)
token = data['token']
print(token)
MichaelAugust
Occasional Contributor III

I'm trying to run the example python script here:

Example: Prepare Esri basemaps for use in offline workflows—Portal for ArcGIS (10.5.x) | ArcGIS Ente... 

and am getting the same error on our 10.5.1 Portal - could you show me how to bypass the SSL error using this script as an example? Thanks!

0 Kudos
MagalyClément-Zaber
Occasional Contributor

Thank you very much, it works !!

0 Kudos