Example Python: Checking the Response Body for New Token
After 24 hours, the authorization token (epimresttoken
)
you created will expire. However, there is a refresh logic used to extend
your authorization. This is managed by the enable-webcm-broker-service
microservice. Basically, your code will check to see if and when a new
epimresttoken
is returned after a
refresh_token
call.
In the Python code example below, a call is made for the next page of data
but with a delay of 60 seconds. If there is too much data, then the
epimresttoken
can expire after 30 minutes. Line 36
checks for a new epimresttoken
in the result body and
sets that to the request header.
To run this Python code:
- Download and install Python.
-
Install the latest version of Requests (the Python HTTP Client) by
going to your computer's command line and running:
python -m pip install requests
-
Once Requests has been installed, use the command line to run:
py .\src\http-testing.py
-
Be sure to set your
<login>
and<password>
, such as in Lines 6 and 7 in the example below.import requests import time from datetime import datetime pageSize = 20 startDatetime = datetime.now() auth = { 'login': '** change me ***', 'password': '** change me ***' } server = 'http://ewapp-stable.home/' reqHeaders = {} def authorize(): auth = { 'login': 'system', 'password': 'system' } print('connecting/logging in') r = requests.post( server + 'enable-api/login', json = auth, timeout=5) print('connected....') result = r.json() ewtoken = result['token']['token'] reqHeaders['ewtoken']= ewtoken reqHeaders.pop('epimresttoken',None ) print( result['token']['token'] ) def reauth(): ra = requests.get( server + 'enable-api/reauth', headers = reqHeaders, timeout = 5 ) raResult = ra.json() success = raResult['success'] if (not success ): print('ewtoken not authorized or expired. Authorizing....' ) authorize() #---------------------- Function to call the item/search api. Return true if this are more records available. def doSearch( page, iteration ): reauth() searchBody = { "repositoryId": 10149, "page": page, "pageSize": pageSize } r = requests.post( server + 'webcm/rest/api/items/search', json =searchBody, headers = reqHeaders, timeout = 5 ) searchResult = r.json() if 'epimresttoken' in searchResult: epimresttoken = searchResult['epimresttoken'] print('setting the epimresttoken ' + epimresttoken ) reqHeaders['epimresttoken'] = epimresttoken ntime = datetime.now() print( 'elapsed='+str( ntime - startDatetime)+' searchResult loop iteration='+str(iteration)+' page=' +str(page)+' length='+str(len(searchResult['data']['itemList'])) + ' of ' + str(searchResult['data']['searchTotal']) ) return searchResult['data']['searchTotal'] > ( pageSize * page ) and searchResult['data']['returnTotal'] == pageSize #-------------------------------------------------------------------------# iteration = 1 onPage = 1 keepGo = 1 while 1: #--- Loop through the repository data. ---* while keepGo: keepGo = doSearch( onPage, iteration ) onPage += 1 time.sleep(60) print('starting loop over again') onPage = 1 keepGo = 1 iteration += 1
If you run the code above and your repository contains over 10,000 records, then your console output will display similar to the following:Notice that after 30 pages of data (30 minutes), the Python program detects the new token and uses it. This logic is around Lines 36 and 38.After 24 hours theewtoken
expires. Thereauth()
discovers this and then reauthorizes the token.