我對Pear :: Auth PHP庫有個疑問。我可以以某種方式「在命令行中登錄」。我的意思是我可以訪問通過Pett :: Auth從HttpRequest保護的資源嗎?我在其他腳本程序中創建自己?你可以給我一個示例(Python,PHP,Java或任何東西可讀)Pear :: Auth從命令行訪問
0
A
回答
0
好,我知道它的工作(希望這可以幫助別人 - 這是Python的解決方案,可轉換爲任何語言,並允許你通過創建的HttpRequest你在你的程序的控制登錄PEAR ::驗證
import urllib
import httplib2
from urllib import urlencode
http = httplib2.Http()
url = 'LOGIN_URL'
# this applies to current version of Pear (had to add the authsecret)
body = {'username': 'USRENAME', 'password': 'PASSWORD', 'authsecret': ''}
headers = {'Content-type': 'application/x-www-form-urlencoded'}
response, content = http.request(url, 'POST', headers=headers, body=urllib.urlencode(body))
# CAREFUL!!! HERE IS THE TRICKY PART !
# response['set-cookie'] looks like this -> 'SomeSession=longID; path=/, SomeSession=longID; path=/, authchallenge=blabla; path=/'
# you need to parse it and use only authchallanage and SomeSessionpart(just one of them I think the last one) - have no idea why, but it works. So next line needs some parsing and fixing
headers = {'Cookie': response['set-cookie']} #parseme -- this is not correnct and will not work, but you have to fix it to match your implementation
print headers
# e.g. headers = {'Cookie': 'MySession=bdfdstiq90oilkpk7n4s2q2g50; authchallenge=fddtggffg5784d359c12dfad4059', 'X_REQUESTED_WITH': 'xmlhttprequest'}#the second header is optional, I needed to access some ajax call
data = dict(argument="to_pass", eg="customerID")
resp, content = http.request("secure_URL", "POST", urlencode(data), headers=headers)
print resp
print content
0
實際上取決於...什麼,並從那裏你正在試圖做到這一點...我想到有些事情:
- 將憑據作爲命令行參數傳遞。
- 將憑證作爲GET請求參數的一部分傳遞(可能假設爲SSL)。
- 使用cURL和餅乾罐。
- 實現一個服務層,以便這些調用以另一種方式處理身份驗證,以便於您的需要(API與身份驗證可與現有的梨認證一起使用)。
0
如果有人有興趣的Java解決方案那就是:
public String getServerJson(){
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost("https://"+hostToReach+"/secure/index.php");
httpost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("username", htmlUsername));
nvps.add(new BasicNameValuePair("password", htmlPassword));
httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
try {
HttpResponse response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
}
catch (Exception e){
e.printStackTrace();
}
HttpPost ajaxPost = new HttpPost("https://"+hostToReach+"/?event_id="+pmp.getPmpEventId().getEventId().toString()+"&categories_only=true&pos=true");
ajaxPost.setHeader("X_REQUESTED_WITH", "xmlhttprequest");
try {
HttpResponse catResponse = httpclient.execute(ajaxPost);
BufferedReader rd = new BufferedReader (new InputStreamReader(catResponse.getEntity().getContent()));
String line = "";
String json = "";
while ((line = rd.readLine()) != null) {
json += line;
}
EntityUtils.consume(catResponse.getEntity());
return json;
}
catch (Exception e) {
e.printStackTrace();
return "";
}
}
注:Cookies是自動處理(HttpClient的將其保持其存在的整個時間,所以你不需要擔心THA t)
相關問題
- 1. Pear Auth驗證()和訪問級別
- 2. 執行命令firebase auth
- 3. 從RStudio訪問命令行(cmd)
- 4. 如何從JScript訪問命令行
- 5. 從命令行訪問SQLite數據庫
- 6. 訪問命令行參數
- 7. 使用system()命令從RStudio訪問命令行(cmd)
- 8. 關於PEAR :: Auth包的問題
- 9. PEAR Auth在Linux機器上的問題
- 10. OSX Dashboard Widget命令行訪問問題
- 11. python google-auth-oauthtool獲取訪問令牌
- 12. 訪問命令WPF
- 13. 從x86 asm訪問linux命令行參數的問題
- 14. 設置命令行選項訪問VBA
- 15. 如何訪問Windows命令行參數?
- 16. 使用gdb訪問命令行參數
- 17. 通過命令行訪問postgreSQL
- 18. 如何訪問命令行參數?
- 19. 遠景programdata命令行訪問
- 20. 命令行訪問谷歌API(日曆)
- 21. 命令行訪問PingFederate控制檯
- 22. 在PHP命令行中訪問會話?
- 23. IBM對象存儲命令行訪問
- 24. 訪問命令行參數在Objective-C
- 25. Powershell訪問命令行參數
- 26. 從WPF中的命令訪問控件
- 27. 如何從viewmodel訪問usercontrol命令
- 28. 如何從python命令訪問linkedin
- 29. 「AUTH NTLM」的SMTP命令
- 30. 從命令行執行Perl的問題
謝謝...我有點意識到它後來...在解決方案上工作。我半... ...無論如何。將完成後發佈 – kosta5 2013-03-14 18:31:05