我对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. 访问命令行
- 3. 从Python Social Auth获取访问令牌
- 4. 执行命令firebase auth
- 5. 从命令行访问JVM属性
- 6. 从命令行访问谷歌日历
- 7. 无法从命令行访问phpmyadmin
- 8. 从RStudio访问命令行(cmd)
- 9. 从P4V访问perforce命令行
- 10. 如何从JScript访问命令行
- 11. 从命令行访问SQLite数据库
- 12. Php sqlite命令行访问
- 13. 访问命令行参数
- 14. 使用system()命令从RStudio访问命令行(cmd)
- 15. PEAR Auth在Linux机器上的问题
- 16. 关于PEAR :: Auth包的问题
- 17. python google-auth-oauthtool获取访问令牌
- 18. 使用PEAR Auth的通知
- 19. 从命令行程序访问REST服务的问题
- 20. 从x86 asm访问linux命令行参数的问题
- 21. 从OSX命令行访问(伪造环境)的问题
- 22. Zend_Cache从命令行保存和从浏览器访问
- 23. 访问AS3中的命令行
- 24. 命令行访问谷歌API(日历)
- 25. IIS7 - 给php访问命令行
- 26. 使用gdb访问命令行参数
- 27. 在PHP命令行中访问会话?
- 28. 如何访问命令行参数?
- 29. 远景programdata命令行访问
- 30. 通过访问网页执行命令?
谢谢...我有点意识到它后来...在解决方案上工作。我半... ...无论如何。将完成后发布 – kosta5 2013-03-14 18:31:05