留言板

How t o make a cURL call to Liferay REST API using auth token ?

Singh S,修改在6 年前。

How t o make a cURL call to Liferay REST API using auth token ?

New Member 帖子: 5 加入日期: 17-7-12 最近的帖子
Hi,

I am developing a portlet and exposing the same over REST using Liferay.
Currently when I have to make a cURL call, I need to pass the username and password as parameter.
  • Is there a way that I can make the cURL call using some sort of Auth token?
  • I also want to expose the login functionality over a cURL call. So that when the user first provides the userName and password, he/she can recieve an Auth token which can then be used for further API calls.


Thanks
thumbnail
Andrew Jardine,修改在6 年前。

RE: How t o make a cURL call to Liferay REST API using auth token ?

Liferay Legend 帖子: 2416 加入日期: 10-12-22 最近的帖子
Hi Singh,

I haven't tried to make a cURL call using Java (I think this is what you are trying to do?) but I have done what you are doing using a cURL from the command line so perhaps what I have done will be of use to you. Here are the steps I performed -- with example curl statements to follow

1. Access the portal -- which will return to you a GUEST cookie
2. Perform a post operation, including the GUEST cookie you received and the user/pass parameters for authentication
3. Step #2 will return to you a USER cookie that is specific to your newly logged in session. You can then cURL the JSON API with the USER cookie as part of the request to have it work.

# get a guest session first.
	curl -s "http://${node}:8080/" -H "'Origin: http://'${node}" --compressed  --insecure -c cookie.txt

	# perform the login
	curl -s "http://${node}:8080/web/guest/welcome?p_p_id=58&p_p_lifecycle=1&p_p_state=maximized&p_p_mode=view&_58_struts_action=%2Flogin%2Flogin" -H "'Origin: http://${node}'" --data "'_58_formDate=${current_time}&_58_saveLastPath=false&_58_redirect=%2Fgroup%2Fcontrol_panel&_58_doActionAfterLogin=false&_58_login=${LIFERAY_USER}&_58_password=${LIFERAY_PASSWORD}&_58_rememberMe=false'" --compressed  --insecure -c cookie.txt -b cookie.txt

        # Do you JSON call
        curl -s 'http://${node}:8080/api/jsonws/...'  -L --compressed --insecure -b authcookie.txt > /dev/null


-- it's not pretty, but that worked for me. If you are doing this in code, then I think you need to perform the same steps, just make sure that the cookie is included in the last request. I think I did this as part of a gradle task for a custom build (using 6.2), but I can't seem to find the code right now.
Singh S,修改在6 年前。

RE: How t o make a cURL call to Liferay REST API using auth token ?

New Member 帖子: 5 加入日期: 17-7-12 最近的帖子
Hey Jardine,

Thanks for the reply. This does work for me.
But shouldn't there be a more direct way similar to Auth token generation and validation in JAX-RS?

Just wondering emoticon

Thanks