Hi,
It is already some time since this post, however I found the following is working (tested on AGE 10.8.1 base deployment with SAML auth):
import arcpy, json
from urllib import request, parse
tokenUrl = arcpy.GetParameterAsText(0)
def getUserInfo(url):
try:
token = arcpy.GetSigninToken()
data = {"token": token["token"], "f": "json"}
req = request.Request(
url,
data=parse.urlencode(data).encode(),
headers={"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
)
response = json.loads(request.urlopen(req).read().decode("utf-8"))
return response["user"]
except Exception as e:
arcpy.AddError(e)
try:
user = getUserInfo(tokenUrl)
arcpy.AddMessage(user)
arcpy.SetParameterAsText(1, user)
except:
arcpy.AddMessage(arcpy.GetMessages())
The tokenUrl parametr is pointing to https://<gis_server_fqdn>/<web_adaptor/rest/self.
This parametr can be set as a constant during the publication of this python script as a GP web tool.
Point the tokenUrl parametr to https://<portal_fqdn>/<web_adaptor>/sharing/rest/portals/self to get more detailed user information.
Edit: if the GP service is running on ArcGIS Enterprise (base deployment), fetching the user info is a one liner
arcpy.GetPortalDescription(arcpy.GetActivePortalURL())["user"]
Matej