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?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Aug 3, 2021 at 0:34
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. 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? 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. 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? Commented Aug 3, 2021 at 21:31
  • The documentation let me whip up a helper function -- enterprise.arcgis.com/en/server/latest/administer/windows/… Commented Aug 4, 2021 at 5:02

1 Answer 1

2

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

answered Mar 16, 2023 at 16:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.