2

I would like to pull data from an ArcGIS Map Service, using Python (ArcPy/ArcGIS Pro), but I'm having trouble getting a token.

My guidance: https://pro.arcgis.com/en/pro-app/latest/arcpy/functions/getsignintoken.htm

This is my code:

import requests
import arcpy
url  = "https://...../arcgis/rest/services/..../MapServer/5"
head = {"Accept": "application/json"}
myFilter = "/query?where=1%3D1&&returnCountOnly=true&f=pjson"
token = arcpy.GetSigninToken()
if token is not None:
    try:
        theToken = token['token']
        theServiceUrl = url + myFilter +  "&token=" + theToken
        response = requests.post(theServiceUrl, headers=head)
        print (response.text)
    except:
        print ("failed")
        exit()

Produces this:

{
 "error": {
  "code": 498,
  "message": "Invalid Token",
  "details": []
 }
}

So the system gave me the token to use, but says it's invalid. What am I doing wrong?

6
  • 2
    You seem to be straddling two different token objects. You can make an HTTPS request through Python or use ArcPy, but you need to follow the documentation for either.
    – Vince
    Commented Aug 3, 2021 at 3:44
  • OK....? I'm not sure I understand. Arcpy is python, no? It's just additional libraries specific to gis. In this code, I'm getting a token using arcpy, and then pushing it back to get the response.... Or not?
    – Maxcot
    Commented Aug 3, 2021 at 7:20
  • 2
    No, you aren't getting a token from the server. You are getting a token from the Portal. The code you have provided hides the URL of the server, but if it isn't the configured Portal, then it's not valid.
    – Vince
    Commented Aug 3, 2021 at 11:55
  • OK, that makes sense. So I have the Portal, and the the ArcGIS REST Services Directory, and they are different. I want to login and get a token from the REST Services directory. Where do I find the documentation about those API endpoints to do that?
    – Maxcot
    Commented Aug 3, 2021 at 21:31
  • The documentation let me whip up a helper function -- enterprise.arcgis.com/en/server/latest/administer/windows/…
    – Vince
    Commented Aug 4, 2021 at 5:02

1 Answer 1

1

The ArcGIS API for Python generates the token and keeps it as a property of the GIS object.

import arcgis
from arcgis.gis import GIS
gis = GIS("https://www.arcgis.com", "jskinner_CountySandbox", "*****")
token = gis._con.token
print(token)‍‍‍‍‍

https://community.esri.com/t5/arcgis-api-for-python-questions/get-the-token-using-arcgis-python-api/td-p/876344

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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