arcgis-python-api icon indicating copy to clipboard operation
arcgis-python-api copied to clipboard

Token Required when accessing layers after GIS connection is made with token

Open jamesrn opened this issue 3 years ago • 1 comments
trafficstars

Describe the bug If using a token to connect to Portal, then some layer methods/classes don't function and return a Token Required message. In this example it is item.layers

To Reproduce Steps to reproduce the behavior: import os import urllib import requests from arcgis.gis import GIS

username='[some username]' password='[some password]' portalURL = f'https://[some portalname]/portal' def createToken(return_expiry=False): url = f"{portalURL}/sharing/rest/generateToken?" global expires print(f'creating token for: {url}') gtValues = {'username' : username, 'password' : password, 'client' : 'requestip', 'expiration' : 28800, # 20 days token 'f' : 'pjson'} gtData = urllib.parse.urlencode(gtValues) gtRequest = requests.post(url, data=gtValues , verify=True) token = gtRequest.json()['token'] expires = gtRequest.json()['expires'] return token

prod_token = createToken() gisconn1 = GIS(portalURL, username=username,password=password) gisconn2 = GIS(portalURL, token=prod_token)

layerid = '<some featurelayer layerID that the user has access too>' portalitem1 = gisconn1.content.search(layerid) portalitem2 = gisconn2.content.search(layerid)

print("Works") featureLayer1 = portalitem1[0].layers print(featureLayer1) print("Fails") featureLayer2 = portalitem2[0].layers

error: Token Required (Error Code: 499)

Screenshots If applicable, add screenshots to help explain your problem.

Expected behavior Expect no difference to behaviour between token connections or user/pass connections.

Platform (please complete the following information):

  • OS: Windows 10
  • Browser n/a
  • conda 4.12.0
  • Python API Version [e.g. 1.6.2] 2.0.1

Additional context Works fine with Python API 1.9.2

jamesrn avatar Aug 05 '22 18:08 jamesrn

The issue is the requestip. Providing a token with requestip is the problem. I suggest you change the requestip for the client to referer and then set the referer to http.

achapkowski avatar Aug 08 '22 10:08 achapkowski