2017-09-04 67 views
0

我想獲得一個有效的會話ID,但迄今沒有成功。如何獲取和使用Rundeck JSESSIONID

我試圖與rundeck API進行工作,password authentication

要提交身份驗證,提交POST請求的網址: $ RUNDECK_SERVER_URL/j_security_check

使用這些參數: 爲j_username :rundeck username j_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用戶界面(http://localhost:4440/user/profile)中看到,然後API調用工作良好,我可以看到歷史記錄。

任何想法我做錯了什麼?或者我錯過了什麼?

謝謝。

回答

1

不要擔心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