RE: How to get the Liferay.authToken value for the current HTML page

Olaf Kock, modified 6 Years ago. New Member Posts: 6 Join Date: 9/26/19 Recent Posts
I 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.

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:
#set($authTokenUtil = $String.class.forName("com.liferay.portal.security.auth.AuthTokenUtil"))
#set($authToken = $authTokenUtil.getAuthToken())
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>
I would like to have the autoToken in the result URL as:

  <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> 


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?
<html>
<head> <title>Some Title</title>
<script type="text/javascript">.....(removed unnecessary text)Liferay.authToken="8Gv0m7IO";Liferay.currentURL="\x2fsubpagename";Liferay.currentURLEncoded="%2Fsubpagename";</script>

I get the following URL when I click on the link: https://www.example.com/news?p_auth=${authToken}
I would like to get the following URL when I click on the link: https://www.example.com/news?p_auth=8Gv0m7IO
However, I tested with hardcoding the assigned value to $authToken and the href code works fine.
#set($authToken = "12345")
<a href="https://www.example.com/news?p_auth=${authToken}">Click</a>
I get what I expect when I click on the link:  https://www.example.com/news?p_auth=12345 

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="

8Gv0m7IO

";) for the current HTML session that I can see in the header session of the HTML code like the following:
<html>
<head> <title>Some Title</title>
<script type="text/javascript">.....(removed unnecessary text)Liferay.authToken="8Gv0m7IO";Liferay.currentURL="\x2fsubpagename";Liferay.currentURLEncoded="%2Fsubpagename";</script>
<body>
………….</body>
</html>
thumbnail
Mohammed Yasin, modified 6 Years ago. Liferay Master Posts: 593 Join Date: 8/8/14 Recent Posts
Hi, 
You can call a javascript function and there you can append the authToken to url   using 
&nbsp;Liferay.authToken available in javascript

Something like this

<script>
var url = "[url=https://www.example.com/news?p_auth=8Gv0m7IO]https://www.example.com/news?+p_auth=[/url]"+Liferay.authToken;
</script>
Lester Ho, modified 5 Years ago. New Member Posts: 6 Join Date: 9/26/19 Recent Posts
I am not familiar with javascript.  May I know what is this section above mean?  Is it part of the URL?  
The section "[url=https://www.example.com/news?p_auth=8Gv0m7IO]" and  the section "[/(slash)url]" that are quoted with the two square brackets?

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? 

Thanks.

thumbnail
Mohammed Yasin, modified 5 Years ago. Liferay Master Posts: 593 Join Date: 8/8/14 Recent Posts
Lester Ho:

I am not familiar with javascript.  May I know what is this section above mean?  Is it part of the URL?  
The section "[url=https://www.example.com/news?p_auth=8Gv0m7IO]" and  the section "[/(slash)url]" that are quoted with the two square brackets?


Its basically your url appending by javascript method which gives authToken.
To get url like this  https://www.example.com/news?p_auth=8Gv0m7IO 
You can use
url="https://www.example.com/news?p_auth="+Liferay.authToken
Lester Ho, modified 5 Years ago. New Member Posts: 6 Join Date: 9/26/19 Recent Posts
Understood.  Thanks.  However, if I do not use Javascript, is there a way to retrieve the authentication token from Liferay using Velocity only?
thumbnail
Olaf Kock, modified 5 Years ago. Liferay Legend Posts: 6441 Join Date: 9/23/08 Recent Posts
Lester Ho:

Understood.  Thanks.  However, if I do not use Javascript, is there a way to retrieve the authentication token from Liferay using Velocity only?
Are you the same Lester who has gotten this answer on stackoverflow 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.
Lester Ho, modified 5 Years ago. New Member Posts: 6 Join Date: 9/26/19 Recent Posts
Yes.  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.

#set($authTokenUtil = $String.class.forName("com.liferay.portal.security.auth.AuthTokenUtil"))
#set($authToken = $authTokenUtil.getAuthToken())
<a href="http://www.example.com/news?p_auth=${authToken}>Click</a>
The above three lines result in the following URL:  <a href="http://www.example.com/news?p_auth=${authToken}">Click</a>

As I mentioned in my initial post, if I manually set the value for the variable $authToken, then the following works as I expected:
#set($authToken = "12345")
<a href="http://www.example.com/news?p_auth=${authToken}">Click</a>
The above two lines will give me the URL that I expect:  <a href="http://www.example.com/news?p_auth=12345"">Click</a>

Therefore, I think I may not understand completely and my two lines below are not correct:
#set($authTokenUtil = $String.class.forName("com.liferay.portal.security.auth.AuthTokenUtil"))
#set($authToken = $authTokenUtil.getAuthToken())

I appreciate if someone can help on my stupid question on Velocity  Thanks in advance.

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.
thumbnail
Olaf Kock, modified 5 Years ago. Liferay Legend Posts: 6441 Join Date: 9/23/08 Recent Posts
Lester Ho:

...Therefore, I think I may not understand completely and my two lines below are not correct:
#set($authTokenUtil = $String.class.forName("com.liferay.portal.security.auth.AuthTokenUtil"))
#set($authToken = $authTokenUtil.getAuthToken())
As the only installation that I have around currently is 7.2, which doesn't like Velocity that much:
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()}

Lester Ho:

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.


Easy: Just add a remark with a hyperlink to the places where you crossposted.
Alla Sapozhnikova, modified 5 Years ago. Junior Member Posts: 82 Join Date: 3/2/12 Recent Posts
<p>Did anybody figure out how to get AuthToken in Velocity in Liferay 7.2?</p>

<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>