title: HLS Stream OneTimePassword (GetHLSToken from 511nj.org)
body: <p>For research purposes, we analyzed and extracted large-scale traffic data from 511nj cameras. Starting from 2020, it adds an OTP(one-time password) to the video link, which is requested by the website API: <a href="https://511nj.org/api/client/camera/getHlsToken?Id=2&amp;rnd=202205281151" rel="nofollow noreferrer">https://511nj.org/api/client/camera/getHlsToken?Id=2&amp;rnd=202205281151</a>, as observed from the network traffic. However, the website itself gets a response of 200 with a valid token, while accessing it by myself gets a response of 401, which indicates an unauthorized request. What makes me confused is why I can get the camera feed with the OTP through the website, but can't access the api. Is there a solution?</p>
  <p>Failed attempt:</p>
  <pre><code>requests.get('https://511nj.org/api/client/camera/getHlsToken?Id=2&amp;rnd='+datetime.datetime.now().strftime('%Y%m%d%H%M')).json()
  </code></pre>
  <p>20220601 Update: Modification has been made based on @Brad 's comment. However, the error message now turns into &quot;An error has occurred&quot;. Please help!</p>
  <pre><code>def GetOTP():
      import requests
      import json
      import datetime
      Now = datetime.datetime.now()
      DT = Now.strftime('%Y%m%d%I%M%S')
      DT24 = Now.strftime('%Y%m%d%H%M')
      Enigma = dict(zip(map(str,range(0,10)),('2','1','*','3','4','5','6','7','8','9')))
      cookies = {
          'ReloadVersion': '112',
          '_ga': 'GA1.2.335422978.1653768841',
          '_gid': 'GA1.2.721252567.1653768841',
      }
      
      headers = {
          'authority': '511nj.org',
          'accept': 'application/json, text/plain, */*',
          'accept-language': 'en-US,en;q=0.9',
          'referer': 'https://511nj.org/Scripts/Directives/HLSPlayer.html?v=V223',
          'responsetype': &quot;&quot;.join([Enigma[key] for key in DT[4:6]])+'$'.join([Enigma[key] for key in DT[6:8]])+'$'.join([Enigma[key] for key in DT[:4]])+'#'+Enigma[DT[9]]+&quot;!&quot;.join([Enigma[key] for key in DT[10:12]])+&quot;!&quot;+DT[-2:]+&quot;#PK&quot;,
          'sec-ch-ua': '&quot; Not A;Brand&quot;;v=&quot;99&quot;, &quot;Chromium&quot;;v=&quot;102&quot;, &quot;Google Chrome&quot;;v=&quot;102&quot;',
          'sec-ch-ua-mobile': '?0',
          'sec-ch-ua-platform': '&quot;macOS&quot;',
          'sec-fetch-dest': 'empty',
          'sec-fetch-mode': 'cors',
          'sec-fetch-site': 'same-origin',
          'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.61 Safari/537.36',
      }
      
      params = {
          'Id': '2',
          'rnd': DT24,
      }
  
      Response = requests.get('https://511nj.org/api/client/camera/getHlsToken?Id=2&amp;rnd='+DT24, headers=headers,cookies=cookies,params=params).json()
  
      print(&quot;&quot;.join([Enigma[key] for key in DT[4:6]])+'$'+''.join([Enigma[key] for key in DT[6:8]])+'$'+''.join([Enigma[key] for key in DT[:4]])+'#'+Enigma[DT[9]]+&quot;!&quot;+''.join([Enigma[key] for key in DT[10:12]])+&quot;!&quot;+DT[-2:]+&quot;#PK&quot;)
      print(params['rnd'])
      print(Response)
      return Response['Data']
  </code></pre>