如何获取和使用 Rundeck JSESSIONID

How to get and use Rundeck JSESSIONID

我正在尝试获取有效的会话 ID,但到目前为止没有成功。

我正在尝试使用 rundeck API,使用 password authentication:

To submit authentication, submit a POST request to the URL: $RUNDECK_SERVER_URL/j_security_check

With these parameters: j_username: rundeck username j_password: password

正如文档所建议的,我是 运行:

>>> import request
>>> jsessionid = requests.post("http://localhost:4440/j_security_check", data=
{"j_username": "admin", "j_password": "admin" }, 
allow_redirects=False).cookies['JSESSIONID']

生成日志:

09/04/2017 03:22:59 PM - DEBUG - Starting new HTTP connection (1): 
10.200.101.21
09/04/2017 03:22:59 PM - DEBUG - http://localhost:4440 "POST 
/j_security_check?j_username=admin&j_password=admin HTTP/1.1" 302 0

但是 jsessionid 对下一个 api 调用永远无效,例如:

>>> response = requests.post("http://localhost:4440/api/14/project/my_project/hi
story" , headers={ "Content-Type": 'application/xml', 'X-Rundeck-
Auth-Token': jsessionid })
>>> response.content
"<result error='true' apiversion='20'><error code='unauthorized'>
<message>(Token:lhefo****) is not authorized for: /api/14/project/weekly_builds/history</message></error></result>"

但是,如果 jsessionid 我正在使用我可以在 Rundeck UI(在 http://localhost:4440/user/profile 中)看到的令牌,那么 API 调用工作正常并且我得到了历史。

对我做错了什么有什么想法吗?或者我缺少什么?

谢谢。

不用担心 JSESSIONID。你应该使用 requests.Seesion()

import requests
s = requests.Session()
r = s.post("URL:PORT/j_security_check", data={"j_username": "user", "j_password": "pass"})
r.status_code # this should be 200
r.url # this should be rundeck home

r = s.get("URL:PORT/api/11/projects",headers = {'Accept': 'application/json'})
r.json() # you should see all your project