<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to automatically refresh a token in 4.5 in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-to-automatically-refresh-a-token-in-4-5/m-p/752746#M69561</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Im trying to refresh an ArcServer token in the JS API, but can't seem to figure it out.&lt;/P&gt;&lt;P&gt;This gist is: I want to refresh the server token every hour automatically without requiring more input from the user while within the same session (ie. the user hasn't closed the browser window).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Right now I&amp;nbsp;get the token with a basic POST request to the server and then register that token with the ID manager like this:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;IdentityManager&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;registerToken&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;{&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp; server&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt; "&lt;SPAN class="comment token"&gt;//exampleUrl/server/rest/services/",&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp; token&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt; localStorage&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;getItem&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'token'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
&lt;SPAN class="punctuation token"&gt;}&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;;&lt;/SPAN&gt;‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;That's it and it works just fine, but when the token expires in an hour (which we want), I don't have a way to simply refresh the token. Since I'm not storing the password like I am the username, I can't simply fire the POST request again to generate a new token.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've looked at creating a credential object which has a refreshToken() method, but can't get it to work, and to be honest I don't fully understand the credential class as it relates to the ID manager, serverInfos, and authentication in general. I tried replacing the above code to register the token with ID manager with the following code with no success:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;var&lt;/SPAN&gt; serverInfo &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;new&lt;/SPAN&gt; &lt;SPAN class="token class-name"&gt;ServerInfo&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;;&lt;/SPAN&gt;
serverInfo&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;server &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; "&lt;SPAN class="comment token"&gt;//exampleUrl/server1";&lt;/SPAN&gt;
serverInfo&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;tokenServiceUrl &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"exmapleUrl/rest/generateToken"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;;&lt;/SPAN&gt;

&lt;SPAN class="keyword token"&gt;let&lt;/SPAN&gt; idObject &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;{&lt;/SPAN&gt;serverInfos&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;serverInfo&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;}&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;let&lt;/SPAN&gt; credential &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;{&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp; server&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"exampleUrl/server1/"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp; ssl&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;true&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp; token&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt; localStorage&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;getItem&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"token"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp; userId&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt; localStorage&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;getItem&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"username"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
&lt;SPAN class="punctuation token"&gt;}&lt;/SPAN&gt;

idObject&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;credentials &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;credential&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;;&lt;/SPAN&gt;
IdentityManager&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;initialize&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;idObject&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;;&lt;/SPAN&gt;‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;(used&amp;nbsp;&lt;A _jive_internal="true" href="https://community.esri.com/thread/119908" target="_blank"&gt;this&lt;/A&gt;&amp;nbsp;as a guide, but I'm not sure &lt;A href="https://community.esri.com/migrated-users/3099" target="_blank"&gt;Jeff Pace&lt;/A&gt;&amp;nbsp;was using the 4.x API)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Does anyone have any examples of using the refreshToken() method, or any insight into the best way to accomplish an automatic token refresh within the API?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 07:57:13 GMT</pubDate>
    <dc:creator>AndrewLindley</dc:creator>
    <dc:date>2021-12-12T07:57:13Z</dc:date>
    <item>
      <title>How to automatically refresh a token in 4.5</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-to-automatically-refresh-a-token-in-4-5/m-p/752746#M69561</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Im trying to refresh an ArcServer token in the JS API, but can't seem to figure it out.&lt;/P&gt;&lt;P&gt;This gist is: I want to refresh the server token every hour automatically without requiring more input from the user while within the same session (ie. the user hasn't closed the browser window).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Right now I&amp;nbsp;get the token with a basic POST request to the server and then register that token with the ID manager like this:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;IdentityManager&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;registerToken&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;{&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp; server&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt; "&lt;SPAN class="comment token"&gt;//exampleUrl/server/rest/services/",&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp; token&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt; localStorage&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;getItem&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'token'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
&lt;SPAN class="punctuation token"&gt;}&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;;&lt;/SPAN&gt;‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;That's it and it works just fine, but when the token expires in an hour (which we want), I don't have a way to simply refresh the token. Since I'm not storing the password like I am the username, I can't simply fire the POST request again to generate a new token.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've looked at creating a credential object which has a refreshToken() method, but can't get it to work, and to be honest I don't fully understand the credential class as it relates to the ID manager, serverInfos, and authentication in general. I tried replacing the above code to register the token with ID manager with the following code with no success:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;var&lt;/SPAN&gt; serverInfo &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;new&lt;/SPAN&gt; &lt;SPAN class="token class-name"&gt;ServerInfo&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;;&lt;/SPAN&gt;
serverInfo&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;server &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; "&lt;SPAN class="comment token"&gt;//exampleUrl/server1";&lt;/SPAN&gt;
serverInfo&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;tokenServiceUrl &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"exmapleUrl/rest/generateToken"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;;&lt;/SPAN&gt;

&lt;SPAN class="keyword token"&gt;let&lt;/SPAN&gt; idObject &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;{&lt;/SPAN&gt;serverInfos&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;serverInfo&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;}&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;let&lt;/SPAN&gt; credential &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;{&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp; server&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"exampleUrl/server1/"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp; ssl&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;true&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp; token&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt; localStorage&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;getItem&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"token"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp; userId&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt; localStorage&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;getItem&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"username"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
&lt;SPAN class="punctuation token"&gt;}&lt;/SPAN&gt;

idObject&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;credentials &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;credential&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;;&lt;/SPAN&gt;
IdentityManager&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;initialize&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;idObject&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;;&lt;/SPAN&gt;‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;(used&amp;nbsp;&lt;A _jive_internal="true" href="https://community.esri.com/thread/119908" target="_blank"&gt;this&lt;/A&gt;&amp;nbsp;as a guide, but I'm not sure &lt;A href="https://community.esri.com/migrated-users/3099" target="_blank"&gt;Jeff Pace&lt;/A&gt;&amp;nbsp;was using the 4.x API)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Does anyone have any examples of using the refreshToken() method, or any insight into the best way to accomplish an automatic token refresh within the API?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 07:57:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-to-automatically-refresh-a-token-in-4-5/m-p/752746#M69561</guid>
      <dc:creator>AndrewLindley</dc:creator>
      <dc:date>2021-12-12T07:57:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to automatically refresh a token in 4.5</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-to-automatically-refresh-a-token-in-4-5/m-p/752747#M69562</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Here is some code I have used in the past to do something similar that might be a starting point.&amp;nbsp; I was using this class in several Web App Builder applications.&amp;nbsp; It did not automatically refresh a token like you are trying to do but intercepted presentation of the user sign in dialog triggered when the IdentifyManager detects a missing or invalid (expired) token.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 11 Dec 2017 20:03:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/how-to-automatically-refresh-a-token-in-4-5/m-p/752747#M69562</guid>
      <dc:creator>JadeFreeman</dc:creator>
      <dc:date>2017-12-11T20:03:37Z</dc:date>
    </item>
  </channel>
</rss>

