I am looking for a good/best-practices example python script the can check for a token for the user, if not valid will request one and then execute a set workspace.
Anyone have anything they are willing to share?
This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.
Unless you need to send the token several times per second, I would simply request a new, short-lived token for every request. They're very quick and easy to get.
Unless you need to send the token several times per second, I would simply request a new, short-lived token for every request. They're very quick and easy to get.
Ok, I can get that; still having a hard time getting a good script to work that does it; getting a 403 constantly.
Trying to write something that I can encapsulate in ArcGIS Server
def do_getToken(): """Call to FME Server to get a token for the GUEST user""" try: url = '{0}/fmerest/v3/tokens'.format(SERVER_URL)
except arcpy.ExecuteError: print(arcpy.GetMessages(2)) except Exception as e: print(e.args[0]) # End do_getToken function
But not a lot of examples for calling FME from python.
I would recommend using the requests library rather than urllib, it is so much easier to work with. I suspect that requests is even installed with some ArcGIS Python interpreters by default. Here's some code that requests a token from FME Server, using the requests module:
def get_token(self): """ Requests a token from FME Server using the username/password :return: Security token """ url = '%s/fmetoken/service/generate' % (self.base_url) payload = { 'user': self.username, 'password': self.password, 'expiration': str(self.token_ttl_secs), 'timeframe': 'second' } response = requests.post(url, data=payload) response.raise_for_status() self.token = response.text return self.token
Granted, it uses the "old" token API, but it should be easy to adapt.
I hope the instance variables are self-explanatory, if not, let me know.