<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; color: rgb(0, 0, 0); font-size: 14px; font-family: Calibri, sans-serif;">
<div><br>
</div>
<div>
<div>
<div>I’m currently using the browser to initially log in to my app. After the user credentials have been validated I intercept the url containing the code and use that on the resource /realms/myrealm/tokens/access/codes. To convert the code into a token. I
am then able to use the embedded access token for subsequent API calls. This works fine until the access token expires. </div>
<div><br>
</div>
<div>When the access token expires the service returns a 401. At present the only way I can get past this is to redirect the user back to the login page and start the above process again (creates another session). I would only expect to have to do this if the
SSO idle timeout and been reached (it hasn’t). I’ve looked through the API docs and found the resource /realms/myrealm/token/refresh. I’ve tried using this resource but have failed miserably. Could someone please explain how to use this resource or point
me somewhere else if I’ve got it completely wrong. </div>
<div><br>
</div>
<div>Example code below. </div>
<div><br>
</div>
<div style="font-family: Calibri;">
<p style="font-family: Monaco; font-size: 11px; margin: 0px;"><span style="color: rgb(147, 26, 104);">private</span> JWSToken refreshToken(String <span style="color: rgb(126, 80, 79);">accessToken</span>, String <span style="color: rgb(126, 80, 79);">refreshToken</span>) <span style="color: rgb(147, 26, 104);">throws</span> IOException,
InvalidTokenException{</p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px;"> JWSToken <span style="color: rgb(126, 80, 79);">token</span> = <span style="color: rgb(147, 26, 104);">null</span>;</p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px; min-height: 15px;"> </p>
<p style="color: rgb(57, 51, 255); font-family: Monaco; font-size: 11px; margin: 0px;">
<span style="color: rgb(0, 0, 0);"> String </span><span style="color: rgb(126, 80, 79);">url</span><span style="color: rgb(0, 0, 0);"> = </span>"<a href="http://127.0.0.1:8080/auth/realms/myrealm/tokens/refresh">http://127.0.0.1:8080/auth/realms/myrealm/tokens/refresh</a>"<span style="color: rgb(0, 0, 0);">;</span></p>
<p style="color: rgb(57, 51, 255); font-family: Monaco; font-size: 11px; margin: 0px;">
<span style="color: rgb(0, 0, 0);"> //String </span><span style="color: rgb(126, 80, 79);">query</span><span style="color: rgb(0, 0, 0);"> = </span>“refresh_token=“<span style="color: rgb(0, 0, 0);"> + </span><span style="color: rgb(126, 80, 79);">refreshToken</span><span style="color: rgb(0, 0, 0);">; </span></p>
<p style="color: rgb(57, 51, 255); font-family: Monaco; font-size: 11px; margin: 0px;">
<span style="color: rgb(0, 0, 0);"> //String </span><span style="color: rgb(126, 80, 79);">query</span><span style="color: rgb(0, 0, 0);"> = </span>“refresh_token=“<span style="color: rgb(0, 0, 0);"> + </span><span style="color: rgb(126, 80, 79);">refreshToken</span><span style="color: rgb(0, 0, 0);"> + </span>“client_id="<span style="color: rgb(0, 0, 0);"> + </span><span style="color: rgb(3, 38, 204);">CLIENT_ID</span>"<span style="color: rgb(0, 0, 0);">;</span></p>
<p style="color: rgb(57, 51, 255); font-family: Monaco; font-size: 11px; margin: 0px;">
<span style="color: rgb(0, 0, 0);"> String </span><span style="color: rgb(126, 80, 79);">query</span><span style="color: rgb(0, 0, 0);"> = </span>"refresh_token="<span style="color: rgb(0, 0, 0);"> + </span><span style="color: rgb(126, 80, 79);">refreshToken</span><span style="color: rgb(0, 0, 0);"> + </span>"client_id="<span style="color: rgb(0, 0, 0);"> + </span><span style="color: rgb(3, 38, 204);">CLIENT_ID</span><span style="color: rgb(0, 0, 0);"> + </span>"grant_type=refresh_token"<span style="color: rgb(0, 0, 0);">;</span></p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px; min-height: 15px;"> //Tried all the above<span class="Apple-tab-span" style="white-space: pre;">
</span></p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px; min-height: 15px;"><br>
</p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px;"> HttpPost <span style="color: rgb(126, 80, 79);">httppost</span> = <span style="color: rgb(147, 26, 104);">new</span> HttpPost(<span style="color: rgb(126, 80, 79);">url</span>);</p>
<p style="margin: 0px;"><font face="Monaco" size="2"> </font><span style="color: rgb(126, 80, 79); font-family: Monaco; font-size: 11px;">httppost</span><font face="Monaco" size="2">.addHeader(</font><font color="#3933ff" face="Monaco" size="2">“Authorization</font><font face="Monaco" size="2"><font color="#3933ff">”</font>, </font><font color="#3933ff" face="Monaco" size="2">“Bearer </font><font face="Monaco" size="2"><font color="#3933ff">“</font> + </font><span style="color: rgb(126, 80, 79); font-family: Monaco; font-size: 11px;">accessToken</span><font face="Monaco" size="2">);
//401 with 400 without</font></p>
<p style="color: rgb(57, 51, 255); font-family: Monaco; font-size: 11px; margin: 0px;">
<span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(126, 80, 79);">httppost</span><span style="color: rgb(0, 0, 0);">.addHeader(</span>"Content-Type"<span style="color: rgb(0, 0, 0);">, </span>"application/x-www-form-urlencoded"<span style="color: rgb(0, 0, 0);">);</span></p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px; min-height: 15px;"> </p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px;"> ByteArrayEntity <span style="color: rgb(126, 80, 79);">entity</span> = <span style="color: rgb(147, 26, 104);">new</span> ByteArrayEntity(<span style="color: rgb(126, 80, 79);">query</span>.getBytes());</p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px;"> <span style="color: rgb(126, 80, 79);">httppost</span>.setEntity(<span style="color: rgb(126, 80, 79);">entity</span>);</p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px;"> HttpResponse <span style="color: rgb(126, 80, 79);">response</span> = <span style="color: rgb(3, 38, 204);">httpclient</span>.execute(<span style="color: rgb(126, 80, 79);">httppost</span>);</p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px; min-height: 15px;"><br>
</p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px;"> <span style="color: rgb(147, 26, 104);">int</span> <span style="color: rgb(126, 80, 79);">status</span> = <span style="color: rgb(126, 80, 79);">response</span>.getStatusLine().getStatusCode();</p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px; min-height: 15px;"><br>
</p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px;"> <span style="color: rgb(147, 26, 104);">if</span> (<span style="color: rgb(126, 80, 79);">status</span> == HttpStatus.<span style="color: rgb(3, 38, 204);">SC_OK</span>){</p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px;"> InputStream <span style="color: rgb(126, 80, 79);">stream</span> = <span style="color: rgb(147, 26, 104);">null</span>;</p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px; min-height: 15px;"><br>
</p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px;"> <span style="color: rgb(147, 26, 104);">try</span>{</p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px;"> <span style="color: rgb(126, 80, 79);">stream</span> = <span style="color: rgb(126, 80, 79);">response</span>.getEntity().getContent();</p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px; min-height: 15px;"><br>
</p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px;"> ObjectMapper <span style="color: rgb(126, 80, 79);">mapper</span> = <span style="color: rgb(147, 26, 104);">new</span> ObjectMapper();</p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px;"> <span style="color: rgb(126, 80, 79);">token</span> = <span style="color: rgb(126, 80, 79);">mapper</span>.readValue(<span style="color: rgb(126, 80, 79);">stream</span>, JWSToken.<span style="color: rgb(147, 26, 104);">class</span>);</p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px;"> }<span style="color: rgb(147, 26, 104);">finally</span>{</p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px;"> <span style="color: rgb(147, 26, 104);">if</span> (<span style="color: rgb(126, 80, 79);">stream</span> != <span style="color: rgb(147, 26, 104);">null</span>){</p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px;"> <span style="color: rgb(126, 80, 79);">stream</span>.close();</p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px;"> }</p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px;"> }</p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px;"> }<span style="color: rgb(147, 26, 104);">else</span>{</p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px;"> <span style="color: rgb(147, 26, 104);">throw</span> <span style="text-decoration: underline; color: rgb(147, 26, 104);">new</span><span style="text-decoration: underline;"> InvalidTokenException()</span>;</p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px;"> }</p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px; min-height: 15px;"> </p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px;"> <span style="color: rgb(147, 26, 104);">return</span> <span style="color: rgb(126, 80, 79);">token</span>;</p>
<p style="font-family: Monaco; font-size: 11px; margin: 0px;"> }</p>
</div>
<div><br>
</div>
<div>
<div></div>
</div>
</div>
<div>
<p class="MsoNormal" style="margin: 0cm 0cm 0.0001pt; font-size: 11pt;"><br>
</p>
</div>
</div>
<br>
<hr>
<font face="Arial" color="Gray" size="2"><br>
Causeway is committed towards reducing its carbon footprint. Please consider the environment before printing this email.<br>
</font><br>
<hr>
<font face="Arial" color="Gray" size="1"><br>
<br>
Disclaimer Notice :-<br>
<br>
The message and any attachments contained in this e-mail are intended for the named recipient(s) only. It may contain privileged or confidential information or information which is exempt from disclosure under the applicable laws. If you are not the intended
recipient(s), you must not read, print, retain, copy distribute, forward or take any or refrain from taking any action in reliance on it or any of its attachments. If you have received or have been forwarded this e-mail in error, please notify us immediately
by return e-mail or telephone (+44 (0)1628 552000) and delete this message from the computer or any other data-reading device in its entirety.<br>
<br>
Please advise us immediately if you do not or your employer does not consent to Internet e-mail for messages of this nature.<br>
<br>
Internet communications cannot be guaranteed to be secure and error-free as the information could be intercepted, corrupted, lost, arrive late or contain viruses. The sender and this Company therefore do not and shall not accept any liability or responsibility
of whatsoever nature in the context of this message and its attachment(s) which arises as a result of Internet transmission. Opinions, conclusion, representations, views and such other information in this message that do not relate to the official business
of this Company shall be understood as neither given nor endorsed by it.<br>
<br>
Registered Office: Comino House, Furlong Road, Bourne End, Buckinghamshire, SL8 5AQ<br>
Registered in England No: 3921897 www.causeway.com<br>
</font>
</body>
</html>