2

I have a geoprocessing service on ArcGIS server 10.3 that is protected with tokens.

This GP Service is a python script. Is it possible to retrieve the user information, such username and password, from inside the script using the token passed as parameter?

Is there a way with arcpy to retrieve user information from a valid token?

2

It is not mentioned anywhere in any esri doc, but there is a way to have the user info from a token. You have to use arcgis/rest/self service like this:

https://<servername>:6443/arcgis/rest/self?f=pjson&token=<token>

From python you can call that service using httplib or other library

import arcpy
import json
from httplib import HTTPConnection
from urllib import urlencode

class MyGPService:
    def __init__(self, config):
        self.token = arcpy.getParameter(0)

    def getUserFromToken(self):
        try:
           params = urlencode({"token": self.token, "f": "json"})
           headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
           httpConn = HTTPConnection(SERVER_BASE_URL, SERVER_PORT)
           httpConn.request("POST", "/arcgis/rest/self", params, headers)
           response = httpConn.getresponse()
           getUserResponse = json.loads(response.read())
           httpConn.close()
        except Exception as e:
           arcpy.AddError(e)

        if "error" in getUserResponse:
           arcpy.AddError(getUserResponse["error"])
        else:
           return getUserResponse["user"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.