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:
        theToken = token['token']
        theServiceUrl = url + myFilter +  "&token=" + theToken
        response = requests.post(theServiceUrl, headers=head)
        print (response.text)
        print ("failed")

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?

  • 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
    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
    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
    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
    Aug 3, 2021 at 21:31
  • The documentation let me whip up a helper function -- enterprise.arcgis.com/en/server/latest/administer/windows/…
    – Vince
    Aug 4, 2021 at 5:02


Your Answer

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

Browse other questions tagged or ask your own question.