How to get the Liferay.authToken value for the current HTML pageHow to get the Liferay.authToken value for the current HTML pagehttps://liferay.dev/en/c/message_boards/find_thread?p_l_id=119785333&threadId=1172983082024-03-28T16:37:56Z2024-03-28T16:37:56ZRE: How to get the Liferay.authToken value for the current HTML pageAlla Sapozhnikovahttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=1191672492020-05-15T18:28:33Z2020-05-15T18:28:33Z<p>Did anybody figure out how to get AuthToken in Velocity in Liferay 7.2?</p><br /><br /><p>Olaf Kock wrote: &quot; As the only installation that I have around currently is 7.2, which doesn&#39;t like Velocity that much: &quot; Does this mean that you cannot get AuthToken in Liferay 7.2, using Velocity?</p>Alla Sapozhnikova2020-05-15T18:28:33ZRE: How to get the Liferay.authToken value for the current HTML pageOlaf Kockhttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=1173391112019-10-02T10:19:09Z2019-10-02T10:19:09Z<div class="quote-title">Lester Ho:</div><blockquote><br />...Therefore, I think I may not understand completely and my two lines below are not correct:<br />#set($authTokenUtil = $String.class.forName("com.liferay.portal.security.auth.AuthTokenUtil"))<br />#set($authToken = $authTokenUtil.getAuthToken())<br /></blockquote>As the only installation that I have around currently is 7.2, which doesn't like Velocity that much:<br />My favorite debugging hack in Velocity was always to print a problematic object's name or toString representation, by just using ${authTokenUtil}.class.getName()} (from memory, might be getClass() instead of class, or something similar). Print this temporarily in your output and see if it matches your expectations. Same with ${authToken.class.getName()}<br /><br /><div class="quote-title">Lester Ho:</div><blockquote><br />Remark: I found this forum afterwards and thought this is a more specialized forum for Liferay and Velocity questions but I did not think of cross-referencing it. Sorry about that. By the way, how do I reference a particular posting on other site? Thanks.</blockquote><br /><br />Easy: Just add a remark with a hyperlink to the places where you crossposted.Olaf Kock2019-10-02T10:19:09ZRE: How to get the Liferay.authToken value for the current HTML pageLester Hohttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=1173384022019-10-02T09:11:37Z2019-10-02T09:11:37ZYes. I posted my follow up question on Stackoverflow that I could not get it to work in Velocity but I did not hear back from anyone. I do not know what I wrote in my Velocity code is wrong following the answer posted to my question on Stackoverflow or if there is anything else needed before the two #set statements below to get the $String.class.forName("com.liferay.portal.security.auth.AuthTokenUtil") to work correctly or if the statement #set($autoToken = $authTokenUtil.getAuthToken()) is incorrect because it does not return the authToken that is in the HTML between the <head> </head> tags. The resulting URL contains the exact string http://www.example.com/news?p_auth=${authToken} without replacing the ${authtoken} with the actual value of the authToken.<br /><br />#set($authTokenUtil = $String.class.forName("com.liferay.portal.security.auth.AuthTokenUtil"))<br />#set($authToken = $authTokenUtil.getAuthToken())<br /><a href="http://www.example.com/news?p_auth=${authToken}>Click</a><br />The above three lines result in the following URL: <a href="http://www.example.com/news?p_auth=${authToken}">Click</a><br /><br />As I mentioned in my initial post, if I manually set the value for the variable $authToken, then the following works as I expected:<br />#set($authToken = "12345")<br /><a href="http://www.example.com/news?p_auth=${authToken}">Click</a><br />The above two lines will give me the URL that I expect: <a href="http://www.example.com/news?p_auth=12345"">Click</a><br /><br />Therefore, I think I may not understand completely and my two lines below are not correct:<br />#set($authTokenUtil = $String.class.forName("com.liferay.portal.security.auth.AuthTokenUtil"))<br />#set($authToken = $authTokenUtil.getAuthToken())<br /><br />I appreciate if someone can help on my stupid question on Velocity Thanks in advance.<br /><br />Remark: I found this forum afterwards and thought this is a more specialized forum for Liferay and Velocity questions but I did not think of cross-referencing it. Sorry about that. By the way, how do I reference a particular posting on other site? Thanks.Lester Ho2019-10-02T09:11:37ZRE: How to get the Liferay.authToken value for the current HTML pageOlaf Kockhttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=1173377342019-10-02T08:42:03Z2019-10-02T08:42:03Z<div class="quote-title">Lester Ho:</div><blockquote><br />Understood. Thanks. However, if I do not use Javascript, is there a way to retrieve the authentication token from Liferay using Velocity only?</blockquote>Are you the same Lester who has gotten <a href="https://stackoverflow.com/a/58080450">this answer on stackoverflow</a> to the same question a few days before you asked here? If so, this is a great demonstration why crossposting is discouraged, generating duplicate effort and (if done nevertheless) should always contain references to all crossposts in all directions.Olaf Kock2019-10-02T08:42:03ZRE: How to get the Liferay.authToken value for the current HTML pageLester Hohttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=1173373602019-10-02T02:50:19Z2019-10-02T02:50:19ZUnderstood. Thanks. However, if I do not use Javascript, is there a way to retrieve the authentication token from Liferay using Velocity only?Lester Ho2019-10-02T02:50:19ZRE: How to get the Liferay.authToken value for the current HTML pageMohammed Yasinhttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=1173252692019-09-30T12:12:26Z2019-09-30T12:12:26Z<div class="quote-title">Lester Ho:</div><blockquote><br />I am not familiar with javascript. May I know what is this section above mean? Is it part of the URL? <br />The section "[url=<a href="https://www.example.com/news?p_auth=8Gv0m7IO">https://www.example.com/news?p_auth=8Gv0m7IO</a>]" and the section "[/(slash)url]" that are quoted with the two square brackets?<br /><p style="text-align: left"></p></blockquote><br />Its basically your url appending by javascript method which gives authToken.<br />To get url like this <a href="https://www.example.com/news?p_auth=8Gv0m7IO">https://www.example.com/news?p_auth=8Gv0m7IO</a> <br />You can use<br />url="<a href="https://www.example.com/news?p_auth=8Gv0m7IO">https://www.example.com/news?p_auth=</a>"+Liferay.authTokenMohammed Yasin2019-09-30T12:12:26ZRE: How to get the Liferay.authToken value for the current HTML pageLester Hohttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=1173239592019-09-30T02:30:45Z2019-09-30T02:30:45ZI am not familiar with javascript. May I know what is this section above mean? Is it part of the URL? <br />The section "[url=<a href="https://www.example.com/news?p_auth=8Gv0m7IO">https://www.example.com/news?p_auth=8Gv0m7IO</a>]" and the section "[/(slash)url]" that are quoted with the two square brackets?<br /><br /><p style="text-align: left">Also if I do not use javascript, is there a method that I can retrieve the Liferay.authToken using Velocity directly instead of using javascript? </p><p style="text-align: left">Thanks.</p>Lester Ho2019-09-30T02:30:45ZRE: How to get the Liferay.authToken value for the current HTML pageMohammed Yasinhttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=1173059572019-09-27T14:32:45Z2019-09-27T14:32:45Z<html><head></head><body>Hi, <br>You can call a javascript function and there you can append the authToken to url using <br><pre><code>&nbsp;Liferay.authToken available in javascript</code></pre><br><pre><code>Something like this</code></pre><br><pre><code><script>
var url = "[url=https://www.example.com/news?p_auth=8Gv0m7IO]https://www.example.com/news?+p_auth=[/url]"+Liferay.authToken;
</script></code></pre></body></html>Mohammed Yasin2019-09-27T14:32:45ZHow to get the Liferay.authToken value for the current HTML pageLester Hohttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=1172983072019-09-27T01:13:04Z2019-09-27T01:13:04ZI am trying to retrieve the Liferay.authToken in the that can be seen in the HTML code on the webpage so that I can use it to create an <a> tag with that Liferay.authToken as a variable in the HREF string.<br /><br />I tried the following in my Velocity template to try to retrieve the authToken of the current session but it does not seem to retrieve anything for $authToken and the string "$authToken" is actually used in the href code instead of the value assigned to $authToken in the set statement:<br />#set($authTokenUtil = $String.class.forName("com.liferay.portal.security.auth.AuthTokenUtil"))<br />#set($authToken = $authTokenUtil.getAuthToken())<br />The result URL is: <a [url=http://href="https://www.example.com/news?p_auth=${authToken}>Click</a>]href="https://www.example.com/news?p_auth=${authToken}">Click</a><br />I would like to have the autoToken in the result URL as: <p style="text-align: left"><span style="color: #2b4259"><span style="font-size: 18px;"> <a [url=http://href="https://www.example.com/news?p_auth=8Gv0m7IO">Click</a>]href="https://www.example.com/news?p_auth=8Gv0m7IO">Click</a> </span></span></p><br />What should I do in order to retrieve the Liferay.authToken value (which is "0H4mKLWq" in this example as seen in the following towards the end of the following code Liferay.authToken="0H4mKLWq";) for the current HTML session that I can see in the header session of the HTML code?<br /><html><br /><head> <title>Some Title</title><br /><script type="text/javascript">.....(removed unnecessary text)Liferay.authToken="8Gv0m7IO";Liferay.currentURL="\x2fsubpagename";Liferay.currentURLEncoded="%2Fsubpagename";</script><br /><br />I get the following URL when I click on the link: <a href="">https://www.example.com/news?p_auth=${authToken}</a><br />I would like to get the following URL when I click on the link: <a href="http://href=%22https//www.example.com/news?p_auth=8Gv0m7IO%22%3EClick%3C/a%3E">https://www.example.com/news?p_auth=8Gv0m7IO</a><br />However, I tested with hardcoding the assigned value to $authToken and the href code works fine.<br />#set($authToken = "12345")<br /><a href="https://www.example.com/news?p_auth=${authToken}">Click</a><br />I get what I expect when I click on the link: <a href="https://www.example.com/news?p_auth=12345">https://www.example.com/news?p_auth=12345</a> <br /><br />What should I do in order to retrieve the Liferay.authToken value (which is "0H4mKLWq" in this example as seen in the following towards the end of the following code Liferay.authToken="<p style="text-align: left"><span style="color: #2b4259"><span style="font-family: "Source Sans Pro",sans-serif"><span style="font-size: 18px;">8Gv0m7IO</span></span></span></p>";) for the current HTML session that I can see in the header session of the HT