<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: get username of geoprocessing service user in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/get-username-of-geoprocessing-service-user/m-p/1119878#M63050</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;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):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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())&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The tokenUrl parametr is pointing to &lt;A target="_blank" rel="noopener"&gt;https://&amp;lt;gis_server_fqdn&amp;gt;/&amp;lt;web_adaptor/rest/self&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;This parametr can be set as a constant during the publication of this python script as a GP web tool.&lt;/P&gt;&lt;P&gt;Point the tokenUrl parametr to&amp;nbsp;&lt;A target="_blank" rel="noopener"&gt;https://&amp;lt;portal_fqdn&amp;gt;/&amp;lt;web_adaptor&amp;gt;/sharing/rest/portals/self &lt;/A&gt;to get more detailed user information.&lt;/P&gt;&lt;P&gt;Edit: if the GP service is running on ArcGIS Enterprise (base deployment), fetching the user info is a one liner&lt;/P&gt;&lt;LI-CODE lang="python"&gt;arcpy.GetPortalDescription(arcpy.GetActivePortalURL())["user"]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Matej&lt;/P&gt;</description>
    <pubDate>Wed, 24 Nov 2021 15:18:47 GMT</pubDate>
    <dc:creator>MatejVrtich</dc:creator>
    <dc:date>2021-11-24T15:18:47Z</dc:date>
    <item>
      <title>get username of geoprocessing service user</title>
      <link>https://community.esri.com/t5/python-questions/get-username-of-geoprocessing-service-user/m-p/1077725#M61705</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;we are running several GP-services on ArcGIS Server, authentication is web-tier based on our companys Active Directory. I try to get the script to be aware of who started the service request (similar to editor tracking on feature classes). I found these two posts, where the conclusion is, that it's not easily possible:&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/t5/python-questions/username-of-geoprocessing-service-user/td-p/491209" target="_blank"&gt;https://community.esri.com/t5/python-questions/username-of-geoprocessing-service-user/td-p/491209&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/t5/python-questions/is-there-any-way-to-get-the-username-of-user-logged-into-arcgis/m-p/113388" target="_blank"&gt;https://community.esri.com/t5/python-questions/is-there-any-way-to-get-the-username-of-user-logged-into-arcgis/m-p/113388&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Anything new on this topic or anyone else having the same problem? Can't be that hard for ESRI to implement, without me having to use SOIs, which I have no clue of.&lt;/P&gt;&lt;P&gt;Thanks and Best regards&lt;BR /&gt;- Florian&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jul 2021 09:29:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-username-of-geoprocessing-service-user/m-p/1077725#M61705</guid>
      <dc:creator>FlorianSteiner</dc:creator>
      <dc:date>2021-07-12T09:29:39Z</dc:date>
    </item>
    <item>
      <title>Re: get username of geoprocessing service user</title>
      <link>https://community.esri.com/t5/python-questions/get-username-of-geoprocessing-service-user/m-p/1078022#M61717</link>
      <description>&lt;P&gt;Did you look at the job info json option as mentioned here?&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/t5/python-questions/is-there-any-way-to-get-the-username-of-user-logged-into-arcgis/m-p/113404/highlight/true#M8886" target="_blank"&gt;https://community.esri.com/t5/python-questions/is-there-any-way-to-get-the-username-of-user-logged-into-arcgis/m-p/113404/highlight/true#M8886&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jul 2021 20:40:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-username-of-geoprocessing-service-user/m-p/1078022#M61717</guid>
      <dc:creator>nzjs</dc:creator>
      <dc:date>2021-07-12T20:40:11Z</dc:date>
    </item>
    <item>
      <title>Re: get username of geoprocessing service user</title>
      <link>https://community.esri.com/t5/python-questions/get-username-of-geoprocessing-service-user/m-p/1078617#M61730</link>
      <description>&lt;P&gt;Yes, I saw that. But it looks like this file is only available when the service is run asynchronous. I also would need the jobID and I currently have no idea of how to get it within python.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jul 2021 11:49:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-username-of-geoprocessing-service-user/m-p/1078617#M61730</guid>
      <dc:creator>FlorianSteiner</dc:creator>
      <dc:date>2021-07-14T11:49:34Z</dc:date>
    </item>
    <item>
      <title>Re: get username of geoprocessing service user</title>
      <link>https://community.esri.com/t5/python-questions/get-username-of-geoprocessing-service-user/m-p/1119878#M63050</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;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):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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())&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The tokenUrl parametr is pointing to &lt;A target="_blank" rel="noopener"&gt;https://&amp;lt;gis_server_fqdn&amp;gt;/&amp;lt;web_adaptor/rest/self&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;This parametr can be set as a constant during the publication of this python script as a GP web tool.&lt;/P&gt;&lt;P&gt;Point the tokenUrl parametr to&amp;nbsp;&lt;A target="_blank" rel="noopener"&gt;https://&amp;lt;portal_fqdn&amp;gt;/&amp;lt;web_adaptor&amp;gt;/sharing/rest/portals/self &lt;/A&gt;to get more detailed user information.&lt;/P&gt;&lt;P&gt;Edit: if the GP service is running on ArcGIS Enterprise (base deployment), fetching the user info is a one liner&lt;/P&gt;&lt;LI-CODE lang="python"&gt;arcpy.GetPortalDescription(arcpy.GetActivePortalURL())["user"]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Matej&lt;/P&gt;</description>
      <pubDate>Wed, 24 Nov 2021 15:18:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-username-of-geoprocessing-service-user/m-p/1119878#M63050</guid>
      <dc:creator>MatejVrtich</dc:creator>
      <dc:date>2021-11-24T15:18:47Z</dc:date>
    </item>
    <item>
      <title>Re: get username of geoprocessing service user</title>
      <link>https://community.esri.com/t5/python-questions/get-username-of-geoprocessing-service-user/m-p/1215811#M65680</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/644"&gt;@MatejVrtich&lt;/a&gt;,&amp;nbsp;&lt;STRONG&gt;getUserInfo&lt;/STRONG&gt; function is not working for &lt;EM&gt;standalone&lt;/EM&gt; ArcGIS Server 10.8.1.&lt;/P&gt;&lt;P&gt;arcpy.GetSigninToken() always returning &lt;STRONG&gt;None&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;Did I miss anything? For standalone ArcGIS Server scenario how to get the token that was used to execute in arcpy based GP service (Synchronous)?&lt;/P&gt;</description>
      <pubDate>Sat, 24 Sep 2022 10:54:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-username-of-geoprocessing-service-user/m-p/1215811#M65680</guid>
      <dc:creator>Ranga_Tolapi</dc:creator>
      <dc:date>2022-09-24T10:54:08Z</dc:date>
    </item>
  </channel>
</rss>

