Esri / ArcREST

python package for REST API (AGS, AGOL, webmap JSON, etc..)
Apache License 2.0
192 stars 155 forks source link

Invalid expired Tokens #266

Closed DShokes closed 7 years ago

DShokes commented 8 years ago

ArcRest or ArcRestHelper

Both

Version or date of download

3.5.5

Bug or Enhancement

Token should be renew if necessary before a function requiring it is executed.

Repo Steps or Enhancement details

I have an ongoing script that queries a hosted layer regularly to perform work locally. At a certain point, the token for the FeatureLayer instance expires and the script comes to a crashing stop.

{u'error': {u'message': u'Invalid token.', u'code': 498, u'details': [u'Invalid token.']}}]

I have tried to include this function in my loop:

def updatetoken(flayer):
    '''obtains a fresh token'''
    if isinstance(flayer,FeatureLayer):
        minutes_til_expired = (flayer.securityHandler.tokenExperationDate - datetime.datetime.now()).total_seconds()/float(60)
        if minutes_til_expired < 20:
            flayer.securityHandler._generateForTokenSecurity(<USERNAME>,<PASSWORD>))
    return

However, I still seem to get an invalid token error every few hours. Is it possible for the FeatureLayer class to handle its own token renewal before executing functions like query? I can't seem to account for every http call with a token across all my modules. Thanks.

DShokes commented 8 years ago

Since ArcGIS Online credentials can't be defined in arcpy, I should mention that some modules of the script involve arcpy exporting pdf's of an mxd with a layer referencing a hosted service layer. The invalid token message still occurs with a module that only imports arcrest, arcresthelper, and pyodbc,

MikeMillerGIS commented 8 years ago

The security handler is designed to refresh the token when it expires. Can you build a repo case and share it with us?

DShokes commented 8 years ago
File "..\env\lib\site-packages\arcrest\agol\services.py", line 1774, in query raise ValueError(result)
ValueError: {u'error': {u'message': u'Invalid token.', u'code': 498, u'details': [u'Invalid token.']}}

Occurs after I run the following for 2 hours as a stand alone script.

def testtoken(fl):
    #run this until the expiration date occurs
    while True:
        print('Current Time: ', datetime.datetime.now())
        print('Token Obtained: ', fl.securityHandler.tokenObtainedDate)
        print('token Expires: ',fl.securityHandler.tokenExperationDate)
        print('Feature Count: ', fl.query(returnCountOnly=True))
    return
def main():
    fl = gis_FeatureLayer()
    testtoken(fl)
    return
if __name__ == '__main__':
    main()

gis_FeatureLayer() returns a FeatureLayer Object that is created the same way as this sample