Access secured Feature Service

575
2
12-12-2018 01:35 PM
MeghanKulkarni
New Contributor III

Hello,

I'm using below code to access FeatureService published using ArcGIS Server. REST endpoint is access protected.

import urllib, httplib
def getToken(username, password, serverName, serverPort):
    # Token URL is typically http://server[:port]/arcgis/admin/generateToken
    tokenURL = "/arcgis/tokens/generateToken"

    # URL-encode the token parameters:-
    params = urllib.urlencode({'username': username, 'password': password, 'client': 'requestip', 'f': 'json'})
    
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
    
    # Connect to URL and post parameters
    httpConn = httplib.HTTPConnection(serverName, serverPort)
    httpConn.request("POST", tokenURL, params, headers)
    
    # Read response
    response = httpConn.getresponse()
    if (response.status != 200):
        httpConn.close()
        print "Error while fetch tokens from admin URL. Please check the URL and try again."
        return
    else:
        data = response.read()
        httpConn.close()
           
        # Extract the token from it
        token = json.loads(data)        
        return token['token']

if __name__ == '__main__':
    token = getToken('<username>','<password>','<host>','6443')
    print token

I get below error:

socket.error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

I'm able to access this Feature Service if I put this URL in a browser and enter the username, password. 

Am I missing something in the above code?

My Best,

Meghan Kulkarni 

0 Kudos
2 Replies
DanielCota1
Occasional Contributor

Hi Meghan Kulkarni

Are you by chance able to provide the actual code you are using when running? You can of course use fake usernames/passwords/URLs, but I just wanted to ensure that there was no syntax issues.

Beyond that, I also wanted to ask if you are executing the code on the same machine that you are able to access the feature service URL from in a web browser. If not, then is the machine that you are executing from connected within the same network as the ArcGIS Server? Or do you know if it has access to that server?

Thanks,

Daniel

MeghanKulkarni
New Contributor III

I got that working Dan. Thanks for heads up.

0 Kudos